Creating controls at runtime -I
Many times we are faced with a situation where we want to
create controls such as TextBox , CommandButton, Label at runtime in Visual
Basic . Say u wanted to create a textBox or an array of Option Buttons but you
don\'t know how many u might need at that point in the program . Creating
controls at runtime allows you the flexibility to do this and more. You can
create and use all the common controls that u see in your toolbar very easily.
The first step in creating a
control at runtime involves declaring a variable which will \'hold\' the
control.
1. Declaration:
It is always better to declare this variable in the general declaration
section of a form so that
it can be used through out the form or declare it globally in a
module(.bas file), if u have a
module added to your project. A good idea here is to name the variable using
standard conventions.
Using the txt prefix for a TextBox cmd prefix for a CommandButton lbl
prefix for a Lable chk prefix for a CheckBox opt prefix for an
OptionButton and so on. For e.g.
Dim txtInput
Dim cmdInput
Dim lblInput
The Next Step involves setting the variable to a
particular control like TextBox or a CommandButton
2. Preparing the variable to hold the control:
This is the most important step while creating a control at runtime. The
common format for
creating a control is as follows
Set varname=frmName.Controls.Add(Control Type,Control Name,frmName)
Here varname is the variable to which you want to set the control to ,frmName
is the form name to
which you want to add the control, Control Type is the type of control i.e "VB.TextBox"
for a text
box,"VB.CommandButton" for a command button and so on .Control Name can be the
same as the
variable name or any name.
So if u wanted to create a textbox txtInput you would have to do it this way
Set txtInput=frmTest.Controls.Add("VB.TextBox","txtInput",frmTest)
To create a CommandButton
Set cmdInput=frmTest.Controls.Add("VB.CommandButton","cmdInput",frmTest)
To create a Label
Set lblInput=frmTest.Controls.Add("VB.Label","lblInput",frmTest)
To create a CheckBox
Set chkInput=frmTest.Controls.Add("VB.CheckBox","chkInput",frmTest)
To create an OptionButton
Set optInput=frmTest.Controls.Add("VB.OptionButton","chkInput",frmTest)
Similarly you can add a ListBox,ComboBox,PictureBox etc
3. Setting the properties of the control.
Well now that you have created the control ,you want it to be displayed,
visible.You will need to
set a few properties before you can display the control. The 2 most important
properties are the
controlname.Left and controlname.Top properites. These 2 properties determine
where your control
will be placed on the form. It is generally a very good idea to set these
properties with respect
to the form on which they are present. For ex
txtInput.Left=frmTest.Left + 100 Or
txtInput.Left=frmTest.Left/2
and
txtInput.Top=frmTest.Top + 100 Or
txtInput.Top=frmTest.Top/2
There are 2 more properites which are equally important.They are the
controlname.Width and
controlname.Height properites.
txtInput.Height=25
txtInput.Width=50
In addition you may set any properties that u might need.
4. Displaying the control.
This is the last step where you have got to set the .Visible property to true
in order to display
the control.
Eg
txtInput.Visible=True
cmdInput.Visible=True
The final code should look something like this if u want to add a textbox at
runtime
In the General Declaration
Dim txtInput
And in any event like the form_load event or command_click event for e.g. put
this
Set txtInput=frmTest.Controls.Add("VB.TextBox","txtInput",frmTest)
txtInput.Left=frmTest.Left/2
txtInput.Top=frmTest.Top/2
txtInput.Height=25
txtInput.Width=50
txtInput.Visible=True
After you have created the controls you can use them as you use your controls
normally. You can set the caption, get the text inputted just as you you would
do for any control created at design time.Many of you must be wondering that
now We have created the controls and can set properties, but how do we react
to the events of the controls, How do we detect if the new commandbutton that
was created is clicked.This will be discussed in the next in this series of
article