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.
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.
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
To create a CommandButton
To create a Label
To create a CheckBox
To create an OptionButton
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.Top=frmTest.Top + 100 Or
There are 2 more properites which are equally important.They are the controlname.Width and
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 final code should look something like this if u want to add a textbox at runtime
In the General Declaration
And in any event like the form_load event or command_click event for e.g. put this
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