Question Creating Command Button ans assigning click event through code?????

B1G M4TTY B

New member
Joined
Jan 25, 2011
Messages
1
Programming Experience
Beginner
I am trying to write a program using Visual Basic 2008 Express Edition that will automaticlay generate multiple Command Buttons that have different Texts and perform different commands.

For example I want to generate Button_1 and Button_2 where Button_1 when clicked enters the value "1" into a textbox and Button_2 enters the value "2" into the same textbox.
I know this can be simple done by using the designer and wirting the code in the Button_1_Click, but what I want to eventually achieve is for various buttons to be generated across different Tabs in a TabControl.

So far I can create a button on a windows form using code, but the button has no purpose so command is assigned to it. The code i've used is listed below:

--------------------------------------------------------
Dim Button As New Button()
'declaring the button, Button1
Button.Name = "Name"
'setting name of button'
Button.Text = "Text"
'setting the text to be displayed on the Button
Button.Location = New Point(100 + 30 , 50 + 30)
'setting the location for the Button where it should be created
Button.Size = New Size(75, 23)
'setting the size of the Button
Me.Controls.Add(Button)
'adding the Button that is created to the form
'the Me keyword is used to refer to the current object, in this case the Form
--------------------------------------------------------

If anyone has any idea how to do this that would be great, I've search the net for hours and cant find what im looking for.

Cheers
 
u can use this :

VB.NET:
Dim Button As New Button()
'declaring the button, Button1
Button.Name = "Name"
'setting name of button'
Button.Text = "Text"
'setting the text to be displayed on the Button
Button.Location = New Point(100 + 30 , 50 + 30)
'setting the location for the Button where it should be created
Button.Size = New Size(75, 23)
'setting the size of the Button

addhandler button1.click addressof button1_click()
'button1 = your button name

Me.Controls.Add(Button)

private sub button1_click(byval sender as object, byval e as system.eventargs)

      'put your code here for button1 click

end sub

hope can help u with that.. lol.. :p
 
Back
Top