Hello.

I am trying to add new button during runtime. Here is the scenario:

when I click 'Food' button, the fields from food column in MySQL database will appear as button.

newbieproblems

To see how this works, start with a new project. Drop a button control onto the blank form and double click on the button. This will create an empty Click event handler that looks like

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

End Sub

Edit the Sub so it looks like the following

Private Sub Button_Click(sender As System.Object, e As System.EventArgs)
    Dim btn As Button = sender
    MsgBox("clicked " & btn.Name)
End Sub

You can rename the Sub to something a little less generic but for now let's just stay with Button_Click. We've removed the Handles Button1.Click part because we'll be assigning the handler at run time.

You can now delete the button from the form. Double click on the form to open a new event handler for Form Load. In this you can add some code as follows

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

    Dim btn As New Button
    btn.Text = "My Button"
    btn.Name = "MyButton"
    btn.Location = New Point(20, 20)
    btn.Size = New Size(75, 23)

    AddHandler btn.Click, AddressOf Button_Click
    Me.Controls.Add(btn)

End Sub

You declare a new button control and set the location, size, etc. properties. In order to respond to events you assign a handler with AddHandler. The final step is to add the new control to the Controls collection of the form (or whatever other container is on that form). You can use the same handler to respond to multiple buttons as long as you ensure that you can distinguish between the buttons. You could do this by setting a unique name (btn.Name), label (btn.Text) or even by using the btn.Tag property.

Try the following

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

    Dim btn As New Button
    btn.Text = "My Button 1"
    btn.Name = "MyButton1"
    btn.Location = New Point(20, 20)
    btn.Size = New Size(75, 23)

    AddHandler btn.Click, AddressOf Button_Click
    Me.Controls.Add(btn)

    btn = New Button
    btn.Text = "My Button 2"
    btn.Name = "MyButton2"
    btn.Location = New Point(20, 60)
    btn.Size = New Size(75, 23)

    AddHandler btn.Click, AddressOf Button_Click
    Me.Controls.Add(btn)

End Sub

and you'll see two buttons.

thank you for replying.
I believe the method you show me is just adding button during run time.
but, where will i add the query?
Scenario:
We have class advisory category,
class advisory has many class sections
if Teacher1 handles 2 sections and he clicks the class advisory category (button)
two buttons will appear. section1 and section2.

You can read this post, from which you can get a conception to load objects dynamically with the help of a query from database.

That's easy:
1. Create a new instance of button
2. Set proper name and perhaps other properties
3. Add it to the corresponding container.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.