Hi - me again...

I have a large list of names that are stored in an array. I want to load the names in groups of 30 and have a button named after each name. The button is on another form that is 'shown' when the names are all loaded.

I can't find a way of referencing the name to refer to the button by...

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        For i = 1 To 30
            Form2.button & (i).text = AAMName(i))

    End Sub

This is how I tried to reference it, but it fails.

Currently I am having to load each button name manually, not using the loop, but using the hideously long...

Form2.Button1.Text = AAMName(i)
Form2.Button2.Text = AAMName(i + 1)
Form2.Button30.Text = AAMName(i + 29)

I have over 1000 names to use!

2 Years
Discussion Span
Last Post by Begginnerdev

You could create the buttons in a method then add them to the form:

For i = 0 To AANAME.Count - 1
    Dim btn As New Button

    With btn
        .Name = "btn" & i
        .Size = New Drawing.Size(70, 20)
        .Text = "CLICK ME"
    End With


Then create a sub procedure for that Form's ControlAdded Event to set the location properties.

For example:

  Private Sub MyForm_ControlAdded(sender As Object, e As System.Windows.Forms.ControlEventArgs) Handles Me.ControlAdded
        Dim pLastControl As Drawing.Point = Me.Controls(Me.Controls.Count - 1).Location

        e.Control.Location = New Drawing.Point(pLastControl.X, pLastControl.Y + 25) '20 pixels for button size and 5 pixels padding

        'Will add each control as follows

        'Code can be changed as desired to place controls in desired location.
    Catch ex As Exception
    End Try
End Sub
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.