Tuesday, November 24, 2009

2 Obscure Javascript Features You Probably Don't Know About

I was going through the w3c javascript tutorial (I like the try it yourself sections ... yes - I am that kind of geek) and I spotted 2 pretty basic but obscure ECMAScript Javascript features I didn't know about.

Feature 1: the '===' operator!
That's right, that's a triple equal. What's it for? Well, you know javascript is not strongly typed, so you can cast a variable from a number to a string just assigning values to it. This triple = operator checks for equality of both value and type. Smart, uh? Also kinda horrible.

Feature 2: variable re-declaration!
Apparently you can re-declare a variable and the javascript interpreter couldn't care less. Moreover, if you re-declare a variable it will retain the same value as the previous homonymous variable had. Handy uh? Except it's plain twisted wrong.

Anyway, God bless ECMAScript Javascript, glorification of all sorts of butchers, and the genius dude who invented it.

Monday, November 16, 2009

[.NET] How to swallow warnings

Well aware this is against all best practices, here's how you suppress all warnings coming from a given region of code:

#pragma warning disable

//All the warnings in here are swallowed!

#pragma warning restore

This is way beyond cool - and might help you out when you can't change your code but you're being hovered by a frustrating policy of ZERO WARNING TOLERANCE.

Go forth and get it done.

Tuesday, November 3, 2009

Quick and Dirty WPF ListView Customization

When in need of customizing a WPF ListView without much WPF experience, chances are you'll hit the wall soon enough on pretty trivial tasks.

This is a collection of recent posts appeared on this blog on the topic. I think the list covers most of the common basic ListView customization tasks.
Hope this'll spare a good share of frustration to some of you cheap guys out there, pulling your hair trying to write xaml by hand rather than using some version of Expression Blend or the likes (as I did myself 'cause the 30-days trial expired and the boss wouldn't show the money).

kick it on DotNetKicks.com

Sunday, November 1, 2009

[.NET] How to bring window upfront

The stuff on this post can be easily integrated with a previous post:
How to Check if App is Already Running.

In order to bring a window upfront you need to mess with win32 API calls - no easy way around (if you know any please give me a shout). Here's how to import the calls we'll be using:
//Win32 API calls to raise a given processs main window
[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")]
private static extern bool IsIconic(IntPtr hWnd);

private const int SW_RESTORE = 9;
Here's a static method to bring a Window upfront in a given winApp (can be easily integrated with the previous post linked above):
static class Program
{

// all the rest of the stuff ... Main other methods etc.

public static void BringOnFront()
{
Process myProcess = Process.GetCurrentProcess();

string myAsseblyName = Assembly.GetExecutingAssembly().GetName().Name;

foreach (Process processId in Process.GetProcessesByName(myAsseblyName))
{
if (myProcess.Id != processId.Id)
{
IntPtr hWnd = processId.MainWindowHandle;

if (IsIconic(hWnd))
{
ShowWindowAsync(hWnd, SW_RESTORE);
}

SetForegroundWindow(hWnd);

break;
}
}
}


}
That'd be all - knock yourself out (tested on a number of XP and Vista machines).