How to: Author composite controls
Composite controls can be employed in many ways. You can author them as part of a Windows desktop application project, and use them only on forms in the project. Or you can author them in a Windows Control Library project, compile the project into an assembly, and use the controls in other projects. You can even inherit from them and use visual inheritance to customize them quickly for special purposes.
To author a composite control
In Visual Studio, create a new Windows Application project, and name it DemoControlHost.
On the Project menu, click Add User Control.
In the Add New Item dialog box, give the class file (.vb or .cs file) the name you want the composite control to have.
Select the Add button to create the class file for the composite control.
Add controls from the Toolbox to the composite control surface.
Place code in event procedures, to handle events raised by the composite control or by its constituent controls.
Close the designer for the composite control, and save the file when you are prompted.
On the Build menu, click Build Solution.
The project must be built in order for custom controls to appear in the Toolbox.
Use the DemoControlHost tab of the Toolbox to add instances of your control to
Form1
.
To author a control class library
Open a new Windows Control Library project.
By default, the project contains a composite control.
Add controls and code as described in the procedure above.
Choose a control you do not want inheriting classes to change, and set the Modifiers property of this control to Private.
Build the DLL.
To inherit from a composite control in a control class library
On the File menu, point to Add and select New Project to add a new Windows Application project to the solution.
In Solution Explorer, right-click the References folder for the new project and choose Add Reference to open the Add Reference dialog box.
Select the Projects tab and double-click your control library project.
On the Build menu, click Build Solution.
In Solution Explorer, right-click your control library project and select Add New Item from the shortcut menu.
Select the Inherited User Control template from the Add New Item dialog box.
In the Inheritance Picker dialog box, double-click the control you want to inherit from.
A new control is added to your project.
Open the visual designer for the new control and add additional constituent controls.
You can see the constituent controls that were inherited from the composite control in your DLL, and you can alter the properties of controls whose Modifiers property is Public. You cannot change the properties of the control whose Modifiers property is Private.
See also
.NET Desktop feedback