Create User Defined Controls in C#

Let's create a User Control.

Start Visual Studio 2005.

File -New-Project Type Visual C#.


Select Windows Control Library from the Templates.

Specify Name with NumericTextBox and save on proper location.

Visual Studio Opens with Blank Control.


Right Click on Blank Control and View Code


Inherit TextBox control by modifying System.Windows.Forms.TextBox on UserControl.

Add code in control to restrict for non-numeric input by user.


Open UserControl1.Designer.cs from Solution Explorer and pass a comment to the line, shown in below:


Now, Build – Build NumericTextBox.

Build Succeeded.

How to use a user defined control, such as the NumericTextBox, in a Windows application.

Create a Windows application with the name PhoneDictionary.


Select a ToolBox and right-click on All Windows Forms then click on Choose items.

.NET Framework Components by default shown after Choose Items...


Now, browse to the location where NumericTextBox.dll has been saved; select NumericTextBox.dll, click the open button and to close choose Toolbox Items.


Add the User Control to the form.


Click on Start Debugging or press F5.


Only numeric input has been accepted in the textbox shown (NumericTextBox). The User Control can be used many more times in an application/project in Visual Studio.

Similar Articles