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 8 Months Ago 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.
I have a 2d matrix with dimension (3, n) called A, I want to calculate the normalization and cross product of two arrays (b,z) (see the code please) for each column (for the first column, then the second one and so on).
the function that I created to find the ...
Write a C program that should create a 10 element array of random integers (0 to 9). The program should total all of the numbers in the odd positions of the array and compare them with the total of the numbers in the even positions of the array and indicate ...
Hi. so this is actually a continuation from another question of mineHere but i was advised to start a new thread as the original question was already answered.
This is the result of previous question answered :
code for the listbox - datagridview interaction
At the top of the code ...