Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

The following code creates a new command button, at run time, named btnTest1. How can i add events(like btnTest1_click) to this object? How can i add code that hadles the events for this object? :(

Dim myButton As Button = New Button()

Me.Controls.Add(myButton)

myButton.Name = "btnTest1"

myButton.Size = New Size(45, 45)

myButton.Location = New Point(10, 10)

Posted

Here is a complete sample of what Machaira suggested:

 

   Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
       Dim myButton As System.Windows.Forms.Button = New System.Windows.Forms.Button
       myButton.Location = New System.Drawing.Point(10, 10)
       myButton.Name = "btnTest1"
       myButton.Size = New System.Drawing.Size(175, 43)
       myButton.TabIndex = 0
       myButton.Text = "Click me, please."
       With myButton
           AddHandler .Click, AddressOf myButton_Click
       End With
       Me.Controls.Add(myButton)
   End Sub

   Private Sub myButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
       Dim ClickedButton As Button = sender
       ClickedButton.Text = "I have been clicked!"
   End Sub

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...