You must follow several rules to be able to create and remove controls with control
First you must have a control of the type that you will be adding placed on
the desired form at runtime. If you will be adding TextBox controls to a form,
for example, you must have at least one text box drawn on that form at design
time. That control can be invisible, and there are no restrictions for the size
or placement of that control, but it must be on the form at design time. This
control will be the template for text boxes that you will add at runtime.
The second requirement for using dynamic control arrays is that the template
object that you draw at design time must be part of a control array. Usually it
is the only control of its type with its Index property set to a value of 0. Continuing
with the text box example, you can have a form with only one text box as a template,
and that text box must have its Index property set to some integer value (typically
0 or 1). If your application required it, you might have additional text boxes
with Index values of 1, 2, 3, and so on.
As long as you have a control array with at least one object in it, you can
create additional instances of that object dynamically at runtime.
After you have built a form with a control that has its Index property set,
you can add additional controls to the control array at runtime. Assume, for example,
that you have an application with one form, Form1, and one text box, Text1. Text1
has an Index value of 0. At runtime, you can create additional instances of Text1
on your form with code such as:
Load Text1 (index)
NOTE:- Index Property Is Blank by Default By
default, the Index property of a control is blank, meaning that it is not part
of a control array.
where Index is an integer value that will be used as the index of the new
text box. When you run the application and want to create the first dynamic instance
of Text1, you would use:
Load Text1 (1)
because Index value 0 is already in use by the Template control. After you
have loaded the new control on the form, you must set the appearance as desired.
You have to set the Visible property to True, if you want the control to appear
to the user. Also you must change either the Left or Top properties in order to
place the control on the form (otherwise the control will show up in exactly the
same spot as the previous control). Finally you can change the Height, Width,
and any other property that can be altered at runtime.
Code for dynamically adding labels to a control array might look like the
code in Listing 4.8.
LOADING LABELS INTO A CONTROL ARRAY
' load a new control into the control array
i = i + 1
' position the new control on the form and add a caption
lblTemplate(i).Left = lblTemplate(0).Left
lblTemplate(i).Top = lblTemplate(i - 1).Top + _
lblTemplate(i - 1).Height + 100
lblTemplate(i).Caption = "index = " _
' make the control visible
lblTemplate(i).Visible = True
After you have added controls to a control array on a form, you may need to
remove them at some point. To remove a control that you have dynamically added
to a control array, you simply code the following:
where Text1 is the name of the control you loaded previously, and Index is
the integer value of that control's Index property.
It is important to remember that you cannot use the Unload statement to remove
a control on the form that was added at design time—this will cause a runtime
error. Only controls added dynamically can be removed with Unload.
Also, you can't add a control twice using the same Index value, and you can't
delete a control with an Index value that isn't in use. Trying either one of these
stunts in code will generate a runtime error.