Control and Component Designers Part Three – Extending Existing Controls

Using these techniques it’s possible to extend existing controls and add attributes to improve their designer experience. To illustrate this I’ve taken the example of the ComboBoxEx control. This extends the ComboBox control by overriding runtime behaviour to overcome a data binding issue, and adds BeginUpdate and EndUpdate methods to improve performance when filling the list of items.


The designer experience for the control inherits the behaviour of the ComboBox control. Since the ComboBox’s own designer excludes some designer support which the control now implements as of Service Pack 2 such as Fore and Back Colors we can add these to our designer and have the designer insert the code to set the colour. The following block of code implements these properties in the design version of the control:-


#if DESIGN


[Browsable(true),

EditorBrowsable(EditorBrowsableState.Always),

DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]

public new System.Drawing.Color ForeColor

{

get

{

return base.ForeColor;

}

set

{

base.ForeColor = value;

}

}


[Browsable(true),

DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]

public new System.Drawing.Color BackColor

{

get

{

return base.BackColor;

}

set

{

base.BackColor = value;

}

}

#endif


Once built we have a combobox with color support, and because the forms designer in Visual Studio uses a standard ComboBox for drawing it already supports drawing itself with the specified colour.



 


Part 1 | Part 2 | Part 3 | Part 4