User control overview (Windows Forms .NET)
A user control is a collection of Windows Forms controls encapsulated in a common container. This kind of control is referred to as a composite control. The contained controls are called constituent controls. User controls derive from the UserControl class.
User controls are designed like Forms, with a visual designer. You create, arrange, and modify, the constituent controls through the visual designer. The control events and logic are written exactly the same way as when you're designing a Form. The user control is placed on a Form just like any other control.
User controls are usable by the project in which they're created, or in other projects that have reference to the user control's library.
Constituent controls
The constituent controls are available to the user control, and the app user can interact with them all individually at runtime, but the properties and methods declared by the constituent controls aren't exposed to the consumer. For example, if you place a TextBox
and Button
control on the user control, the button's Click
event is handled internally by the user control, but not by the Form where the user control is placed.
Add a user control to a project
After creating a new project, use the Visual Studio templates to create a user control. The following steps demonstrate how to add a user control to your project:
In Visual Studio, find the Project Explorer pane. Right-click on the project and choose Add > User Control (Windows Forms).
In the Name box, type a name for your user control. Visual Studio provides a default and unique name that you may use. Next, press Add.
After the user control is created, Visual Studio opens the designer:
For an example of a working user control, see How to create a user control.
.NET Desktop feedback