Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

Is it possible to program events and subs like the Click Event for a button that was declared without physically being on the form?

 

Example:

 

Dim NewButton as New Button

 

How would I go about firing the click event and using it inside a button that is not physically in the form during design time, but instead a new instance of it being created during runtime?

 

Any help is appreciated,

 

Thanks :D

Edited by Fabian_Russ
  • Administrators
Posted

If you already have the actual event handler code written then you can attach it via the AddHandler keyword.

e.g.

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
       MessageBox.Show("Clicked", "Clicked", MessageBoxButtons.OK, MessageBoxIcon.Information)
   End Sub

   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       Dim b As New Button
       b.Text = "click me"
       b.Location = New Point(10, 10)
       Me.Controls.Add(b)
       AddHandler b.Click, AddressOf Button1_Click
   End Sub

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted
If you already have the actual event handler code written then you can attach it via the AddHandler keyword.

e.g.

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
       MessageBox.Show("Clicked", "Clicked", MessageBoxButtons.OK, MessageBoxIcon.Information)
   End Sub

   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       Dim b As New Button
       b.Text = "click me"
       b.Location = New Point(10, 10)
       Me.Controls.Add(b)
       AddHandler b.Click, AddressOf Button1_Click
   End Sub

 

Wow dude! that's exactly what i'm looking for!!! you're the best!

 

5/5 star rating!!!

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...