Sunday, April 17, 2011

Removing EventHandler from itself

Lambda expression offers a great way to write less code for registering event handlers:

But what if we need to remove the event handler after the WriteLine call, from the event handler itself? It is possible, if the lambda expression delegate had a name:

One problem with this though, when initializing the handler variable, inside the code that initializes it, at the line button.Click -= handler we access the handler which at this point is not initialized yet! So, to fix this, simply initialize it with null first (which might look a little bit awkward):

Monday, April 11, 2011

Managed GetLastError

When p/invoking a native method from managed code, it's not ok to call GetLastError to check for an error, because internally the .Net engine can call other API native methods who might override the last error you actually want from the method you invoke. The solutions is Marshall.GetLastWin32Error.

Tuesday, February 15, 2011

Top 10 Programming Quotes

10. If debugging is the process of removing software bugs, then programming must be the process of putting them in. -  Edsger Dijkstra

9. The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time.  – Tom Cargill

8. “There are two ways of constructing a software design. One way is to make it so simple that there are obviously no deficiencies. And the other way is to make it so complicated that there are no obvious deficiencies.”-  C.A.R. Hoare

7. Measuring programming progress by lines of code is like measuring aircraft building progress by weight. – Bill Gates

6. “Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.” – Martin Golding

5. “The trouble with programmers is that you can never tell what a programmer is doing until it’s too late.” – Seymour Cray

4. Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. – Rick Cook

3. “Most of you are familiar with the virtues of a programmer. There are three, of course: laziness, impatience, and hubris.” – Larry Wall

2. “Sometimes it pays to stay in bed on Monday, rather than spending the rest of the week debugging Monday’s code.” – Christopher Thompson

1. Walking on water and developing software from a specification are easy if both are frozen. –Edward V Berard

Source: ginktage.com

Wednesday, September 22, 2010

XAML bind control to code-behind property

Say you want to bind, in Xaml, a control's property (ListView.ItemsSource) to a property defined in code-behind (MyItems), in a window or user control.

First, the code-behind property should be declared as a (normal) Propoerty or a DependecyProperty:

C#:


To bind, in Xaml, the ListView to this property, you need to:
XAML:

Monday, September 13, 2010

Apply the same style to all elements of a particular type

To apply the same style to all the elements of a particular type on a Window or UserControl, say all Labels, you need to specify the TargetType attribute of the Style:

All the labels on the Window will now have the properties specified in this style.

Sunday, September 12, 2010

Using system colors in WPF

What is the best way to use the system colors in a WPF application? Say you want to draw a rectangle with the background color set to one of the system colors, say the color of the active title bar. The main problem is that this color is not the same, it varies with the Windows theme used and might be different from one computer to another.

XAML: