Creating User Defined Controls in VB.NET July 18, 2008
Posted by prasu in : Technical, VB.NET , 4 commentsIn many applications we need tailor-made controls to suit our programming needs. Such controls can be created in VB.NET.
- To create a user-defined control you have to use Windows Control Libray.
Go to file –> New Project–> Windows Forms Contol Library –> (Give any desired name to your project) –> Ok. - You will find a Canvas. Now using the controls present in the toolbox create the required component (control).
- Code the Control as you want it to work.
- You can add user defined properties by using property keyword.
- The size of the canvas is the size of the user defined control you are creating. So change the canvas size as required at the end.
- After you have written the code check the control by debugging the project.
- Now go to Build –> Build <component name>.
- To add the Component (control) in the toolbox, right click in the empty area of the Toolbox –> Choose Items –> Click on browse. (This should be in a new VB.NET Windows Application)
Go to the Project Folder (This time, its the Control Library Project you created) –> Bin –> Debug –> Select <componentname>.dll file
Check the Component name in .NET Framework Components Tab –> OK. - The new Component (control) is added to your toolbox.
Ex: Creating a text-flipping banner control.
- Create a windows library form and name it Banner.
- In the Canvas drag two Textboxes and add a Timer.
- Set the Timer’s enabled property true,Set the interval to say 1000 (unit:milliseconds).
- Set some default Text in both the Textboxes.
- Double click on the timer and write the below code.
- Then write two properties named first and second which are used to change the text of the banner.
- Drag one textbox on top of other and then reduce the canvas size such that it covers only the textboxes.
- Debug the program, The Text of the banner oscillates between the two values of the textboxes.You can change the text of the first and second text box using property first and second respectively.
- If it is working properly stop debugging and click on build –> build banner.

Public Class Banner
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Static f As Boolean
If f = True Then
Me.TextBox1.Visible = True
Me.TextBox2.Visible = False
Else
Me.TextBox1.Visible = False
Me.TextBox2.Visible = True
End If
f = Not f
End Sub
Public Property first() As String
Get
Return Me.TextBox1.Text
End Get
Set(ByVal value As String)
Me.TextBox1.Text = value
End Set
End Property
Public Property second() As String
Get
Return Me.TextBox2.Text
End Get
Set(ByVal value As String)
Me.TextBox2.Text = value
End Set
End Property
End Class
In the above code within a property we see two more keywords
get:It returns a value to the caller.
set:It sets the value to the given property.
