When writing a UserControl, it is often desired to expose one or more of the sub-controls design-time support to the user of your control. It is reasonably straight forward to do, and here is a rundown of how:
We start off with our UserControl, in this case the imaginatively named
The code behind looks like this:
The first attribute on the class (
[Designer(typeof(TestControlDesigner))]) just instructs that we want it to use our own custom designer file (which we create in a minute).
The next important point is the addition of the
ToolStrip property, and the
DesignerSerializationVisibility attribute that goes with it. This informs the winforms designer that any changes made to the ToolStrip should be stored in the hosting container's designer file. Without this attribute, no changes made in the designer would persist when you closed the designer.
Next, we add a reference to
System.Design in the project, and create our
TestControlDesigner class, inheriting from ControlDesigner:
As you can see, we have very little in here. The
Initialize method is overriden, and we call
EnableDesignMode on our ToolStrip (the property added to the TestControl earlier).
After compiling, we can go to our form (again, imaginatively named Form1), and add a couple of instances of
TestControl to it from the tool box:
As you can see, the two control's ToolStrips contents is unique, and we have the ToolStrip's designer exposed in the forms designer.