Create User Defined Controls in C#


Let's create a User Control.

Start Visual Studio 2005.

File -New-Project Type Visual C#.

User1.gif

Select Windows Control Library from the Templates.

Specify Name with NumericTextBox and save on proper location.

Visual Studio Opens with Blank Control.

User2.gif

Right Click on Blank Control and View Code

User3.gif

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

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

User4.gif

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

User5.gif

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.

User6.gif

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

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

User7.gif

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

User8.gif

Add the User Control to the form.

User9.gif

Click on Start Debugging or press F5.

User10.gif

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.

Up Next
    Ebook Download
    View all
    Learn
    View all