Windows Forms Programming in C#

Windows Forms Programming in C# by Chris Sells ISBN: 0321116208 An excellent book for learning Windows Forms using C# for both Beginners and experienced Programmers. The presentation is nice and crisp the chapter size is usually around 50 pages, presenting all the necessary details and at the same time maintaining the reader’s interest in the topic being presented.

While deserializing how do I check whether a name is available in the deserialized info?

This is usually an issue when a newer version introduces newer names, then the older version will not serialize property due to the absence of certain names. For example, this code will fail, sometimes: protected MyClassConstructor(SerializationInfo info, StreamingContext context) { … // This might fail if MyProp was added in a newer version and you are serializing an older version. this.MyProp = info.GetBoolean(‘MyProp’); } To avoid such conflicts, you could insert version nos. into the serialized info. and during deserialization check for a name only when a particular version is being deserialized. Or you could instead parse through the available info in the SerializationInfo list as follows: [C#] protected MyClassConstructor(SerializationInfo info, StreamingContext context) { foreach(SerializationEntry entry in info) { switch(entry.Name) { case ‘MyProp’: // This will make sure that older versions without the MyProp name will also deserialize without any problems this.MyProp = (bool)entry.Value; break; … } } } [VB.Net] Protected MyClassConstructor(ByVal info As SerializationInfo, ByVal context As StreamingContext) As Protected Dim entry As SerializationEntry For Each entry In info Select Case entry.Name Case ‘MyProp’ ’ This will make sure that older versions without the MyProp name will also deserialize without any problems Me.MyProp = (Boolean)entry.Value Exit For End Select Next End Function

How can I paint a representation of enum values using a UITypeEditor?

The following HatchStyleEditor class draws a graphical representation for values in the HatchStyle enumeration. HatchStyleEditor is derived from UITypeEditor and overrides the GetPaintValueSupported method and returns true. GetPaintValueSupported indicates that this editor supports the painting of a representation of an object’s value. PaintValue paints the representative value to the canvas. public class HatchStyleEditor : UITypeEditor { public override bool GetPaintValueSupported(ITypeDescriptorContext context) { return true; } public override void PaintValue(PaintValueEventArgs e) { if (e.Value is HatchStyle) { HatchStyle hatch = (HatchStyle) e.Value; Brush br = new HatchBrush(hatch, SystemColors.WindowText, SystemColors.Window); e.Graphics.FillRectangle(br, e.Bounds); br.Dispose(); } } }

At designtime, how can I hide a base class property in my derived control from the user

Set the Browsable attribute on the new property that hides the existing one. Here is a code snippet that hides the WordWrap property in the derived TextBox. [C#] public class MyTextBox : TextBox { [Browsable(false)] public new bool WordWrap { get{ return false;} //always false set{} } } [VB.NET] Public Class MyTextBox Inherits TextBox _ Public Shadows Property WordWrap() As Boolean Get Return False ’always false End Get Set ’empty End Set End Property End Class ’MyTextBox