Updated April 7, 2023
Definition of C# User Control
C# user control is defined as an implementation in programming language of C# to provide an empty control and this control can be leveraged to create other controls. This implementation provides additional flexibility to re-use controls in a large-scale web project. Not only it is about re-usage of controls, but it also helps users to find and pinpoint a bug and then making it easier to resolve the bug in a shorter time. Through user control, one can make any changes in the code at just a single place and the effect will be seen in every web form or the form in the windows application that is attached to the user control. Extra effort and time are the very big benefits using the technique of user control.
Syntax:
Instantiation of the class to be declared as User Control:
public partial class < class name > : UserControl
{
public < class name >
{
…
}
}
Initializing the component of the class used in the user control:
public < class name >
{
InitializeComponent();
}
Returning a selected item from a class in the application form:
get
{
return <component to be returned>
}
How to Create User Control in C#?
Now in order to create a user control, one would prefer creating own controls in addition to using the out-of-box controls available. The creation of controls is categorized as:
• Extended: This is accomplished by extension of any of the existing controls and derives the things which can be reused.
• User: This is accomplished by making groups of several other controls and making sure that the group is able to justify the reason of its creation as a part of the architecture.
• Custom: This is accomplished by drawing a control along with the Graphical Device Interface + (GDI +).
From the above categorization, we would focus our energy on the pointer of user control which is made sure that is performs the required task by normally combining more than one control to form a unit that logically and functionally justifies its requirement of build. The requirement can be either to aid a functionality or even improving the reusability. The user controls look like any other class and the logic is separated from the design aspect of the application. One can create user control inside the project itself but in case one would need to reuse and look for better maintainability, it is recommended to create separate dll or control forms library in windows.
Before we talk about the user control in C#, we need to know the pre-requisite for creation of user control, and for that we need Microsoft Visual Studio to be installed in the system. Once the pre-requisites are fulfilled, we would now look at the step-by-step execution of creation of user control in C#.
1. For this we would have to first create a new project in Visual studio. In the same, we would need to open the Visual studio and select Windows Forms Control Library from the already existing templates in the visual studio and look at the windows application-enabled one.
2. We will now name the project as desired and then click on Next and finally select the version of .NET framework.
3. It will take some time for the form to get initialized and one would land on the design page.
4. From the toolbox on the left-hand side, we would drag a label and fill the text as “Countries” and another element we will drag is combo box.
5. From the solution explorer on the right-hand side, we will add a class named as countriesClass.cs and fill in some code in the class.
6. Now in the main class which in our case is the UserControl1.cs, we will add the snippet we mention in the syntax section on examples.
7. We add a list which contains currencies of the country and the corresponding countries.
8. Finally, when everything is done, we give some final touch to the design on how the user control will look.
9. Lastly, we right-click on the project name in the solution explorer and click on rebuild to generate a dll as a result of the build and would have no errors.
Next, we will look at the 2 classes as mentioned in our steps to finally get a feel of what it looks like.
Examples
Snippets of the different classes to be used in order to make the user control.
Syntax:
countriesClass.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace UserControlDemo
{
public class countriesClass
{
public string currency { get; set; }
public string countryName { get; set; }
}
}
UserControl1.cs
using System;
using System.Collections.Generic;
using System.Security.Permissions;
using System.Windows.Forms;
namespace UserControlDemo
{
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
public countriesClass SelectedCountry
{
get
{
return (countriesClass)countryCbs.SelectedItem;
}
}
private void UserControl1_Load(object sender, EventArgs e)
{
List<countriesClass> list = new List<countriesClass>();
list.Add(new countriesClass() { currency = "USD", countryName = "United States" });
list.Add(new countriesClass() { currency = "INR", countryName = "India" });
list.Add(new countriesClass() { currency = "AUD", countryName = "Australia" });
list.Add(new countriesClass() { currency = "AED", countryName = "United Arab Emirates" });
list.Add(new countriesClass() { currency = "GBP", countryName = "United Kingdom" });
countryCbs.DataSource = list;
countryCbs.ValueMember = "Currency";
countryCbs.DisplayMember = "Country";
}
private void countryCbs_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
Output:
Design of the user control:
dll generation:
Conclusion
In this article we have gone through the steps in fulfilling the task of creating user control in C# and finally, one can use the same user control throughout any other windows form application one would build. Now, if one changes the user control at its root and rebuild the dll, the corresponding application where the dll is present will automatically get reflected with the latest change and hence the reusability!
Recommended Articles
This is a guide to C# User Control. Here we discuss the definition, syntax, How to create user control in C#? examples with code implementation. You may also have a look at the following articles to learn more –