1

I have a single columns table which holds 50 records. I would like to create 50 buttons from those records. How can I do that in Vb.Net?

Any help will be appreciated.

1
  • 1
    Is this winforms, asp.net, wpf, silverlight etc? What information is in the column? Commented Nov 24, 2010 at 13:36

2 Answers 2

1

Assuming you mean Winforms:

Use your Datasource(f.e. a DataTable) and loop its RowCollection. For example:

Private Sub BtnLoadButtons_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnLoadButtons.Click
    Dim source As New DataTable("MyButtonTable")
    source.Columns.Add(New DataColumn("MyButtonColumn", GetType(String)))
    For i As Int32 = 1 To 50
        Dim newRow As DataRow = source.NewRow
        newRow("MyButtonColumn") = "Button_" & i
        source.Rows.Add(newRow)
    Next
    'you are loading the above DataTable from SQL-Server, now iterate the rows...'
    For Each row As DataRow In source.Rows
        Dim btn As New Button()
        btn.Name = DirectCast(row("MyButtonColumn"), String)
        btn.Text = btn.Name
        btn.Location = New Point(0, Me.Panel1.Controls.Count * btn.Height)
        AddHandler btn.Click, AddressOf handleButton
        Me.Panel1.Controls.Add(btn)
    Next
End Sub

Private Sub handleButton(ByVal sender As Object, ByVal e As EventArgs)
    Dim btn As Button = DirectCast(sender, Button)
    'do something ...'
End Sub
Sign up to request clarification or add additional context in comments.

1 Comment

Excellent. Exactly what I am looking for. Thank you very much Mr. Schmelter. Regards
1

If you are working in a Winforms application you can use the FlowLayoutPanel because it automatically orders the button layouts for you:

dim i as integer=1
for each record in Table
    dim btn as new Button
    btn.id = "btn" & i
    i+=1
    Panel1.Controls.add(btn)
next

1 Comment

+1 but you don't just need to use FlowLayout. You can use the i to place your buttons fairly simply where the Location x/y will be a function of i.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.