Advanced UserControl Question

G

Guest

The ToolBar control allows you to add toolbar buttons. When you add a button,
the button is not only displayed in the toolbar, but also added as a
separator control in the form/usercontrol your toolbar exists in.

I'm writing my own usercontrol which contains various different controls
(e.g. Panel, Button). I would like these controls to be listed in the drop
down just above the Properties grid, when a new instance of the usercontrol
is added to the form. This will allow the user to select the controls and
customize them.

I also want to allow the user to able to click on the controls in the
usercontrol to select them and display their property values in the Property
Grid.

Can someone please show me a C# example on how to do this.

Thank you.

Regards
Mark
 
D

Dave Sexton

Hi Mark,

The difference between the ToolBars functionality and your UserControl is that you are explicitly adding the ToolBar buttons at
design-time, but when you include your UserControl its Panel and Buttons are already there. This means that your nested Controls
are not being managed by the designer.

A UserControl is meant to be an encapsulated, composite control. In other words, it contains controls and makes them appear and
function as a single control in the designer.

One option would be to define public properties for each of the controls that you want publicly configurable at design-time:

[Category("Appearance"), TypeConverter(typeof(ExpandableObjectConverter))]
public Button Button1
{
get { return button1; }
}

With the above code button1 will be configurable via the PropertyGrid at design-time through the Button1 property. This does,
however, defeat the purpose of a UserControl to an extent.

You might want to look in to creating a custom designer for your control, but I'm not sure if you'll be able to accomplish your goal
that way:

IDesigner on MSDN:
http://msdn2.microsoft.com/en-us/library/system.componentmodel.design.idesigner.aspx

DesignerAttribute on MSDN:
http://msdn2.microsoft.com/en-us/library/system.componentmodel.designerattribute.aspx
 
G

Guest

Thanks Dave. I'll have a look at the links you sent.

Regards
Mark

Dave Sexton said:
Hi Mark,

The difference between the ToolBars functionality and your UserControl is that you are explicitly adding the ToolBar buttons at
design-time, but when you include your UserControl its Panel and Buttons are already there. This means that your nested Controls
are not being managed by the designer.

A UserControl is meant to be an encapsulated, composite control. In other words, it contains controls and makes them appear and
function as a single control in the designer.

One option would be to define public properties for each of the controls that you want publicly configurable at design-time:

[Category("Appearance"), TypeConverter(typeof(ExpandableObjectConverter))]
public Button Button1
{
get { return button1; }
}

With the above code button1 will be configurable via the PropertyGrid at design-time through the Button1 property. This does,
however, defeat the purpose of a UserControl to an extent.

You might want to look in to creating a custom designer for your control, but I'm not sure if you'll be able to accomplish your goal
that way:

IDesigner on MSDN:
http://msdn2.microsoft.com/en-us/library/system.componentmodel.design.idesigner.aspx

DesignerAttribute on MSDN:
http://msdn2.microsoft.com/en-us/library/system.componentmodel.designerattribute.aspx

--
Dave Sexton

Mark Collard said:
The ToolBar control allows you to add toolbar buttons. When you add a button,
the button is not only displayed in the toolbar, but also added as a
separator control in the form/usercontrol your toolbar exists in.

I'm writing my own usercontrol which contains various different controls
(e.g. Panel, Button). I would like these controls to be listed in the drop
down just above the Properties grid, when a new instance of the usercontrol
is added to the form. This will allow the user to select the controls and
customize them.

I also want to allow the user to able to click on the controls in the
usercontrol to select them and display their property values in the Property
Grid.

Can someone please show me a C# example on how to do this.

Thank you.

Regards
Mark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top