Dynamic creation of controls in vb.net code

4 views
Skip to first unread message

Henry

unread,
Feb 19, 2005, 12:59:02 PM2/19/05
to
Hi,

I have to create a variable amount of controls with events for a windows
form in my vb code.

Private WithEvents T_Label As Label
Private WithEvents T_ButtonDo As Button

I create the controls in a loop in load event of the form:

For i = 0 to NumberOfControls

T_Label = New Label
T_Label.Name = "LabelRecord_" & Format(i, "000")
T_Label.Tag = i
AddHandler T_Label.DoubleClick, AddressOf DoSomething
AddHandler T_Label.Click, AddressOf DoSomething
....
' add button
T_ButtonDo = New Button
T_ButtonDo.Name = "ButtonDo_" & Format(i, "000")

...

Next

Later in code in a another event I want to access the properties of the
previously created controls again:

For i = 0 to NumberOfControls

' for exambple
T_ButtonDo.Text = "Some value"

Next

The problem is that the code above of course always adresses the last
created control of the Loop in the Load event! How can I adress all controls
created in the loop with their properties and methods ?

Building an array does not solve the problem as I cannot use events together
with an array of controls! And I need the events.

Thanks a lot for help in advance!!!
--
Best regards

Henry

Ken Tucker [MVP]

unread,
Feb 19, 2005, 4:22:53 PM2/19/05
to
Hi,

You are creating one control over and over again. You dont need
withevents if you use addhandler. Why dont you try something like this

Dim arLabels as new arraylist
dim arButtons as new arraylist

For i = 0 to NumberOfControls

dim t_Label as label


T_Label = New Label
T_Label.Name = "LabelRecord_" & Format(i, "000")
T_Label.Tag = i
AddHandler T_Label.DoubleClick, AddressOf DoSomething
AddHandler T_Label.Click, AddressOf DoSomething

arLabels.add(T_Label)
....
' add button
dim T_Button as button


T_ButtonDo = New Button
T_ButtonDo.Name = "ButtonDo_" & Format(i, "000")

arButton.Add(T_Button)
...

Next

Later in code in a another event I want to access the properties of the
previously created controls again:

For i = 0 to NumberOfControls

' for example
directcast(arButton(i),Button).Text = "Some value"

Next


Ken
--------------------
"Henry" <henry...@nospam.nospam> wrote in message
news:FC219D7F-1A59-453F...@microsoft.com...

Cor Ligthert

unread,
Feb 20, 2005, 3:59:17 AM2/20/05
to
Henry,

I have made a new sample for your question (my previous was using an array
of controls).
This is without that. (There is no need because the form holds an collection
itself).

(When you need to access only those controls as well directly, it is easier
to make an array first before we misunderstood each other, because in the
control collection contains all controls on a form)

\\\Needs only a new windowform and pasting this in
Private Sub Form6_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
For i As Integer = 0 To 5
Dim bt As New Button
bt.Location = New Drawing.Point(8, 8 + i * 24)
bt.TabIndex = i
bt.Text = i.ToString
bt.Tag = i.ToString
AddHandler bt.Click, AddressOf ClickButton
Controls.Add(bt)
Next
End Sub
Private Sub ClickButton(ByVal sender As Object, _
ByVal e As EventArgs)
MessageBox.Show("Clicked was " & _
DirectCast(sender, Button).Tag.ToString)
End Sub
///

I hope this helps a little bit?

Cor


Reply all
Reply to author
Forward
0 new messages