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.
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.
Edited by Reverend Jim
Just assuming that you use datgridview to display data and also just assuning you mean cells in the datagridview have a look at:
If this is what you want follow also the references there.
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?
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.
1. Create a new instance of button
2. Set proper name and perhaps other properties
3. Add it to the corresponding container.
So I have a JTextfield in the cells of 1 of the columns in my JTable
I want to allow only numbers and up to one dot/period. Doesant ...
vb.Net - Regular Expression Tester
Every now and then I find another use for a regular expression. For those not familiar with regular expressions, they can be as cryptic to ...
I'm trying to build a client and a server in the same program. For example, user 1 sends a packet of data to user 2, user 2 after receiving the ...