Hi

I understand how to create a button in runtime in vb.net

my code to do this is:

' create button
Dim btn As New Button
' set some button properties
btn.Text = "go"
btn.Top = 45
btn.Left = 190
'add button to form
Me.Controls.Add(btn)

I read on a forum from daniweb that I should add a handler to my button. This is the part I am struggeling with. I dont know how to add the handler or how to use the handler

Add the handler:

the code information I looked up showed that the handler should be something in the lines of:

AddHandler btn.Click, AddressOf Me. ????

im not sure what should be entered where I inserted the ????

Use the handler:

Ferthermore I am not sure on how to use this button once it has a handler attached

so to do somethin simple lets just say I want to open a new form with the button
the code wil be:

dim frm as new Form2
frm.show()

How do I call the handler to implement this code?

AddHandler btn.Click, AddressOf Me.(enter your desire sub event)
So your code will look something like this:

Private Sub ClickButton(ByVal sender As Object, ByVal e As EventArgs)
        End
'''your code goes here
End Sub

    Sub creatingBtn()
        btn.Location = New System.Drawing.Point(200, 150)
        btn.Width = 100
        btn.Height = 20
        Me.Controls.Add(btn)
        btn.Text = "Fuck off"
        AddHandler btn.Click, AddressOf Me.ClickButton
    End Sub

ClickButton is the event.

In order to obtain the proper Parameters for your Event Procedure...

(ByVal sender As System.Object, ByVal e As System.EventArgs) ' <--//--- Parameters ---//--

...locate a similar control from the Toolbox and select your event for it.
For this example, I will use the "MouseDown" Event of a Button.

I added a button (Button1) from the Toolbox and located the "MouseDown" Event from the code window.

Private Sub Button1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseDown

    End Sub

The next step is to remove the "Handles Button1.MouseDown" and possibly rename your new Sub Procedure.
The Parameters should be left as is.

Private Sub myCoolDynamicBtn_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)

    End Sub

All that is left is to create new Dynamic Controls and connect them to your new Sub Procedure.

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '// create and customize Dynamic Control.
        Dim btn1 As New Button With {.Name = "btn1", .Text = "my btn1", .Location = New Point(5, 5), .Size = New Size(55, 55)}
        AddHandler btn1.MouseDown, AddressOf myCoolDynamicBtn_MouseDown '// set Handler.
        Me.Controls.Add(btn1) '// add to Form.

        '// create and customize Dynamic Control.
        Dim btn2 As New Button With {.Name = "btn2", .Text = "my btn2", .Location = New Point(75, 5), .Size = New Size(55, 55)}
        AddHandler btn2.MouseDown, AddressOf myCoolDynamicBtn_MouseDown '// set Handler.
        Me.Controls.Add(btn2) '// add to Form.
    End Sub

    Private Sub myCoolDynamicBtn_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        Dim btn As Button = CType(sender, Button)
        MsgBox(btn.Name) '// get the sender's Name.
        If btn.Name = "btn2" Then MsgBox("Add command here.", MsgBoxStyle.Information)
    End Sub
End Class

try this code like this for creating a control with event handlers in vb.net:

Dim btn As New Button
     
    Private Sub btnCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreate.Click
            btn.Size = New Size(150, 50)
            btn.Location = New Point(119, 30)
            GroupBox1.Controls.Add(btn)
            btn.Text = "Click me"
            AddHandler btn.Click, AddressOf ShowWelcomeMessage
    End Sub

you can also handles events at runtime. this is the complete article with example:
http://www.authorcode.com/add-and-remove-action-handler-dynamically/

This article has been dead for over six months. Start a new discussion instead.