.Net

LinQ and Forms

Iterating a Form's controls:
var qry = from c in this.Controls.OfType<Control>()
              select c;
foreach ( var c in qry )
    MessageBox.Show( c.Name + ":" + c.GetType().ToString(), "Control Type" );
The Controls collection is not directly usable in the query - the OfType method performs the necessary conversions. To grab all controls of a specific type:
var q2 = from c in this.Controls.OfType<NumericUpDown>() select c;

Lambda & Forms

Lambda expressions and statements: In the context of Forms programming, is there an advantage over anonymous methods? The lambda form is certainly more succinct and (with a bit of practice) simpler to read. Examples: Some simple callbacks as expressions:
// pop a message
button.Click += ( sender, args ) => MessageBox.Show( "Click" );
// increment a numeric updn
button.Click += ( sender, args ) => number.Value = number.Value + 1;
Here's the same things as anonymous methods:
button.Click += delegate( object sender, EventArgs e ) { MessageBox.Show( "Click" ); }
button.Click += delegate( object sender, EventArgs e ) { number.Value = number.Value + 1; }
Slightly more complex callback as a lambda statement (note the enclosing {}):
this.FormClosing += ( sender, args ) => {
    if ( MessageBox.Show( "Really?", "Quit?", MessageBoxButtons.OKCancel ) != DialogResult.OK )
        args.Cancel = true;
};
And as an anonymous method:
button.Click += delegate( object sender, FormClosingEventArgs args ) {
    if ( MessageBox.Show( "Really?", "Quit?", MessageBoxButtons.OKCancel ) != DialogResult.OK )
        args.Cancel = true;
};
In this context (simple examples) there's very little difference (especially since they apparently compile to nearly identical code) other than brevity. One thing I've found is that my event handlers and EventArgs derivatives tend to have really long names - the brevity of the lambda syntax is very appealing from that standpoint.

Magic Locations

System.Environment.GetFolderPath( Environment.SpecialFolder.XXXXXX ); Where XXXXXX is: * CommonApplicationData -- %ALLUSERSPROFILE%\Application Data * ApplicationData -- %USERPROFILE%\Application Data * CommonProgramFiles -- C:\\Program Files\\Common Files * Cookies * Desktop * DesktopDirectory * Favorites * History * InternetCache * LocalApplicationData -- %USERPROFILE%\\Local Settings\\Application Data * MyComputer * MyDocuments * Personal * Programs -- %USERPROFILE%\\Start Menu\\Programs * StartMenu -- %USERPROFILE%\\Start Menu * Startup -- %USERPROFILE%\\Start Menu\\Programs\\Startup * System -- C:\\WINDOWS\\system32 * Templates * etc.

Serialization

So after stumbling around trying to load objects from an XML file, I see the Serialization package - waaayy cool. It's almost point-and-shoot simple.

I get the serialization working for the project - all very cool - and all of a sudden it stops. Stops cold. Won't load, won't unload. No clues, just a cryptic message that "Can't serialize object".

Hmmmm.... after considerable time trying to figure out what the hell I'd changed, I stumpled on it.

Syndicate content