Understanding VB .NET User Controls

VB.Net user controls are those controls that have other controls in them so that they provide a complex functionality if needed. Combining controls to form a VB.Net user control helps you to modularize the code that you are developing for your project.

User controls are created as .ascx file and these are added to the project in the bin directory of the project. After you create a user control you can add them to the tool palette so that you can use them just like any other control in the tool box.

For example you can combine a label and a textbox to create a user control. So whenever you drag and drop this user control from the toolbox you get a label and a textbox together.

You can use the get and set routines to get the properties of the label and the textbox and to set the properties of the toolbox. Sample ‘get and set’ routine is given below, for a textbox and label combination in a user control.

Public Property sampText() As String
Get
sampText = TextBox1.Text
End Get
Set(ByVal samptxtValue As String)
TextBox1.Text = samptxtValue
End Set
End Property
Public Property sampLbl() As String
Get
sampLbl = Label1.Text
End Get
Set(ByVal samplblValue As String)
Label1.Text = samplblValue
End Set
End Property

Similarly you can use different properties for the controls available in a user control.