Creating User Controls
This page provides a quick lookup sheet for creating a user
control in C#.
You can find a list of property attributes in the
ComponentModule class in the msdn help.
1) In
Visual Studio you must set the following property to ensure the control will
appear in the toolbox

2) Add
a Windows Control Library project to the solution (you will probably only need
one for your entire suite of controls)
3) Add
a reference to your control library project to the Windows Application project
you want to use
4) Design
your control using the Windows designer
5) To
add properties you should use all the following
|
/// <summary>
/// Set the text of
the label
/// </summary>
[Description("Set
the text of the label")]
[Category("Web
Property")]
[DefaultValue("Label1")]
[Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public override
string Text
{
get
{
return _text;
}
set
{
_text = value;
this.lblLabel.Text = _text;
_ResizeControl();
}
}
|
Why so much stuff?
Well the <summary> tags
will ensure your brief description will appear in the intelli-sense.
Description is
what the programmer sees at the bottom of the property Window.
Browsable means
the property will appear in the property Window.
DefaultValue allows
you to set the initial value of the property when it is first added to a form
DesignerSerializationVisibility
(this is not needed for simple properties as by default the value states
‘Visible’). However for complex properties, based on a class you should use the
value (DesignerSerializationVisibility.Content) which means
the object public properties with be preserved when the designed is reloaded
(e.g. the Font property on a text box).
6)
To make the control appear in the toolbox and also provide a default
event, you need to use these class level attributes
|
[System.ComponentModel.ToolboxItem(true)]
[DefaultEvent("NWEventClick")]
public partial
class MyClass
: UserControl
{
|
7) To
make changes to your control actually work in a designer, you need to rebuild
your project, otherwise the old binaries still apply.
8) To
make code only activate when using the designed and not run time, you need to
check this Boolean property. Note this property is not set in the control
initialisation
9) To
debug your code in designer mode – Don’t know – never figured
that out. My control implementations so far are based on guess work L
10) To create a contained
control (i.e. something where you can drag and drop controls onto) use this
class declaration
|
[System.ComponentModel.ToolboxItem(true)]
[Designer("System.Windows.Forms.Design.ParentControlDesigner,
System.Design", typeof(IDesigner))]
public partial
class MyPanel
: UserControl
{
|
Debugging your code in designer mode
This is not obvious, as the thing you actually need to debug
is Visual Studio itself. This Microsoft article tells you what you need to know
(at the end)
http://msdn.microsoft.com/en-us/library/ms996457.aspx
Basically you start an external debugger which is Visual
Studio e.g. it might be
- C:\Program
Files\Microsoft Visual Studio 8\Common7\IDE\devenv.exe
You set this property on the control library project itself.
You will then get a second session of VS. Now this is the weird thing:
You must set the breakpoints in the first instance of VS but
you should do your designer type actions in the second. Cool!