I don't know if this is best practices per se`, but it
works.
The following generates a new button control each time
menu item 1 is clicked and assigns an event handler to
it. Each button uses the same default event handler.
In this case, when a button is clicked I simply display
the TabIndex of the clicked button.
<snip>
Private Sub MenuItem1_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
MenuItem1.Click
Dim newMemberButton As New System.Windows.Forms.Button
With newMemberButton
.Visible = True
.Dock = DockStyle.Left
.Size = New Size(96, 96)
End With
Me.Controls.Add(newMemberButton)
AddHandler newMemberButton.Click, AddressOf
newMemberButton_Click
End Sub
Private Sub newMemberButton_Click(ByVal sender As Object,
ByVal e As System.EventArgs)
Dim tempButton As New Button
tempButton = sender
MessageBox.Show(tempButton.TabIndex)
End Sub
<end snip>
As you can see, you don't have to name each control. I'm
not the expert here, but my best guess is that when you
create the control object and assign it to the forms
control collection, all the form knows about the object
is the Address of it. But thats all it needs.
Hope this helps...
kurt
-----Original Message-----
Is it possible to add controls to a form at runtime. If
so, how would you write the Handles statement for an eventif the program can theoretically load thousands of
controls? I would have to programmatically name the
control so how could I get the event handler to run for
that control if I don't know the name of the control untilruntime?
Thanks
Brent
.