Control array?

As I understand it, there are no control arrays in .net and just collections of controls. IS that right? If so, how do I dynamically add controls at runtime (not knowing before hand how many there are) and set up events for them?

I understand:
[code]Dim btnButton() as Button()
ReDim btnButton(1)
button(0).Tag="FirstButton"
Me.Controls.Add(btnButton(0)[/code]
in Form code, (IF that is correct syntax)but how do I have an event handler that can handle all buttons in the collection and then tell them apart by tag value like the old vb6: [code]Select Case btnButton(Index).Tag?[/code]

Learning new tricks,
James

Comments

  • I have explained this with 2 buttons..for n number of buttons use a counter say i and replace b(0),b(1) with b(i)..

    Dim b() As Button
    ReDim Preserve b(0)
    b(0) = New Button()
    b(0).Text = "FirstButton"
    b(0).Tag = "1"
    AddHandler b(0).Click, AddressOf x
    ReDim Preserve b(1)
    b(1) = New Button()
    b(1).Text = "SecondButton"
    b(1).Tag = "2"
    AddHandler b(1).Click, AddressOf x
    Me.Controls.Add(b(0))
    Me.Controls.Add(b(1))

    Where x will serve as a common procedure for all the buttons as shown here:-

    Private Sub x(ByVal sender As System.Object, ByVal e As System.EventArgs)
    If CType(sender, System.Windows.Forms.Button).Tag = "1" Then
    'use select case if you wish instead of if-end if
    MessageBox.Show("1 was clicked")
    Else
    MessageBox.Show("2 was clicked")
    End If

    End Sub





  • : I have explained this with 2 buttons..for n number of buttons use a counter say i and replace b(0),b(1) with b(i)..
    :
    : Dim b() As Button
    : ReDim Preserve b(0)
    : b(0) = New Button()
    : b(0).Text = "FirstButton"
    : b(0).Tag = "1"
    : AddHandler b(0).Click, AddressOf x
    : ReDim Preserve b(1)
    : b(1) = New Button()
    : b(1).Text = "SecondButton"
    : b(1).Tag = "2"
    : AddHandler b(1).Click, AddressOf x
    : Me.Controls.Add(b(0))
    : Me.Controls.Add(b(1))
    :
    : Where x will serve as a common procedure for all the buttons as shown here:-
    :
    : Private Sub x(ByVal sender As System.Object, ByVal e As System.EventArgs)
    : If CType(sender, System.Windows.Forms.Button).Tag = "1" Then
    : 'use select case if you wish instead of if-end if
    : MessageBox.Show("1 was clicked")
    : Else
    : MessageBox.Show("2 was clicked")
    : End If
    :
    : End Sub
    :
    :
    Thanks, I think I got it. I assume this code would work then, correct?
    [code]
    Public Sub AddButtons(ByVal Num as Integer)
    Dim I as Integer
    Dim b() As Button
    ReDim Preserve b(Num)
    For I = 0 to Num
    b(I) = New Button()
    b(I).Text = "Button " & Cstr(I)
    b(I).Tag = Cstr(I)
    AddHandler b(I).Click, AddressOf x
    Me.Controls.Add(b(I))
    'Code to arrange location on container here
    Next I
    End Function

    'Put generic x here as you demonstrated[/code]
    I assume the Cstr() funtion works on Integer. Is that true also? I'm still fuzzy about converting types. Would this work too:[code]b(I).Text = "Button " & Ctype(I, String)[/code] or [code]b(I).Text = "Button " & I.ToString()[/code]Assuming that Integer has a ToString() method.

    Thanks for the help,
    James
  • Use i.toString() ..
    Your code will work..I have slightly changed it for location of buttons..
    Public Sub AddButtons(ByVal Num As Integer)
    Dim I As Integer
    Dim b() As Button
    ReDim Preserve b(Num)
    For I = 0 To Num
    b(I) = New Button()
    b(I).Text = "Button " & I.ToString
    b(I).Tag = I.ToString
    AddHandler b(I).Click, AddressOf x
    b(I).Location = New System.Drawing.Point(250, 40 + I * 40)
    Me.Controls.Add(b(I))
    Next I
    End Sub
  • : Use i.toString() ..
    : Your code will work..I have slightly changed it for location of buttons..
    : Public Sub AddButtons(ByVal Num As Integer)
    : Dim I As Integer
    : Dim b() As Button
    : ReDim Preserve b(Num)
    : For I = 0 To Num
    : b(I) = New Button()
    : b(I).Text = "Button " & I.ToString
    : b(I).Tag = I.ToString
    : AddHandler b(I).Click, AddressOf x
    : b(I).Location = New System.Drawing.Point(250, 40 + I * 40)
    : Me.Controls.Add(b(I))
    : Next I
    : End Sub
    :

    Thanks!
    James

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion