954,515 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Using a button in runtime

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?

pantoeflou
Newbie Poster
1 post since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

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 = \"#### off"
        AddHandler btn.Click, AddressOf Me.ClickButton
    End Sub


ClickButton is the event.

abcdefg2008
Newbie Poster
2 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

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
codeorder
Posting Virtuoso
1,913 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384
 

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/

hscoder
Newbie Poster
6 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
Infraction Points: 5
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: