Re: Dynamic Control Array
Quote:
Originally posted by VBD
To make a long story short, I'm trying to create a scripting language for the .NET Compact Framework. I want the script to be able to generate buttons on the fly. In VB6, I would have used a control array. Now, I can't. I've done some searching and figured out how to generate controls at runtime which don't have events, and how to hard code several controls to one event. How do I create something like a control array so I can create controls at runtime and use them with an event?
hmm I'm not sure if I got your question right, but anyways:D
VB Code:
Private Sub createControls()
' You can declare this outside the sub to be able to access them directly later on
Dim myButtons(5) As Button
Dim i As Integer
For i = 0 To myButtons.GetUpperBound(0)
myButtons(i) = New Button
' These are not necessary
With myButtons(i)
.Name = "foo" & i.ToString
.Text = "&Button" & i.ToString
' You chose this somehow
.Location = New Point(0, i * myButtons(i).Height)
' sometimes useful when you're making too many controls
' You can use the Tag value later on or just leave it
.Tag = i
End With
AddHandler myButtons(i).Click, AddressOf myButtons_Click
Next
' Add the whole array to the form's controls at once. You can
' add them one by one by calling Controls.Add() too
Me.Controls.AddRange(myButtons)
End Sub
Private Sub myButtons_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim curButton As Button = DirectCast(sender, Button)
MessageBox.Show(curButton.Name & " was clicked")
End Sub
hth