Using Events and Subs for a dimmed object?

Fabian_Russ

Newcomer
Joined
Sep 29, 2008
Messages
23
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
 
Last edited:
If you already have the actual event handler code written then you can attach it via the AddHandler keyword.
e.g.
Visual Basic:
    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
 
If you already have the actual event handler code written then you can attach it via the AddHandler keyword.
e.g.
Visual Basic:
    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!!!
 
Back
Top