|
User Controls
User Controls are the controls which are created by the user and they are based on
the class System.Windows.Forms.UserControl. Like standard
controls, user controls support properties, methods and events. Once a user control
is created it can be added to any form or any number of forms like all other controls.
Creating a User Control
To create a user control select File->New->Project->Visual
Basic Projects and select Windows Control Library from
the templates and click OK. Alternatively, you can add user control to the existing
project by selecting Project->Add User Control. The image
below displays the new project dialogue to add a User Control project.
The form that opens after clicking OK looks like the image below. It looks similar
to a normal form.
Creating a User Control with Example
Drag a Label and a TextBox control from the toolbox onto the new user control form.
The image below displays that.
Double-click the user control form to open it's code behind file. In the code behind
file type the following code below the Load event (under End Sub) of UserControl1
to set the property of the user control which is being created.
Public Property sanText() As String
Get
sanText = TextBox1.Text
End Get
Set(ByVal Value As String)
TextBox1.Text = Value
End Set
End Property
Public Property sanLbl() As String
Get
sanLbl = Label1.Text
End Get
Set(ByVal Value As String)
Label1.Text = Value
End Set
End Property
|
The above code implements the sanText() and sanLbl() properties with a property get/set
pair. Once typing the code is done build the solution using Build->Build Solution from
the main menu to create the .dll (Dynamic Link Library)
file to which we refer to. The dll file also makes this user control available to
other projects. After finishing building the solution we next need to add this
user control to the toolbox to make it available to other projects and forms. To do
that, open a windows form or use an existing form. Right-click on the Windows Form
tab section on the toolbox and click on Customize Toolbox as
shown in the image below.
Clicking on Customize Toolbox opens "Customize Toolbox" dialogue box as shown in the
image below.
On this window select the .NET Framework components tab which displays a list. Click
browse to add the dll of the user control we created and select the dll file from
the bin directory of the Windows Control library project. Once that is done, click
OK. The dll of the user control gets added to the .NET Framework Components. Select
that and click OK. The user control which we created gets added to the toolbox. Now
this control can be used like other controls in the toolbox.
Alternatively, the user control can be added to the toolbox in another way. To do
that, right-click on the References item in the Solution Explorer window and select
Add Reference which opens the Add Reference dialogue box. Select the projects tab
and double-click the User Control item. That adds item to the selected components
box at the bottom of the dialog. click OK to finish.
Using the User Control
The user control, UserControl11 which we created can be added to any form like all
other controls. Open a new form and drag this user control from toolbox to the form.
Drag a Button from the toolbox and place that on the form. Double-click Button
to open it's Click event. Place the following code in the Click event of the Button:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal_ e As
System.EventArgs) Handles Button1.Click
MessageBox.Show("TextBox" & UserControl11.sanText())
MessageBox.Show("Label1" & UserControl11.sanLbl())
End Sub
|
Run the code and enter some text in the TextBox. When
you click the Button, the text entered in the TextBox is displayed in a MessageBox.
That's the way User Controls are used after they are created.
Inherting Controls
One of the easiest ways to create a new control is to inherit from a preexisting control.
When inheriting from an existing control, the newly created control contains all the
functionality of the base control but can also serve as a base for adding new functionality.
In addition to having all the functionality of the base control an inherited control
also inherits the visual representation of the base control. For example, you can
inherit from a TextBox control and implement code to change its appearance.
Unless sealed (NotInheritable), most
Windows Controls can be used as a base class for inheritance. Inheriting from an existing
control is the most easiest way of creating a new control. You can choose this method
if you want to replicate an exisiting windows control and want to add more functionality
to it. You can also use this method if you want to have all the functionality of the
existing control but want to provide a new look and feel.
To create a new Inherited control you must specify a Windows Form control as a base
class for this control. The following sample code creates a textbox that accepts only
numbers as user input.To implement that functionality you must override the OnKeyPress method
as shown below.
Public Class NumberOnlyBox Inherits System.Windows.Forms.TextBox
Protected Overrides Sub OnKeyPress(ByVal e as KeyPressEventArgs)
If Char.IsNumber(e.KeyChar)=False Then
e.handled=True
End If
End Sub
End Class
|
|