Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

click event in code for new button

Status
Not open for further replies.

solo1234

Programmer
Jun 25, 2007
39
NL
Dear vb dotnet users,

Via a class we can make a button but how can be an event inserted?

Hope you can help us.
Michelle.


Used code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
create_button.setButton(Me)
End Sub

Public Class create_button
Public Shared Sub setButton(ByVal frm As Form)
Dim cb As New Button
With cb
.Location = New System.Drawing.Point(50, 50)
.Size = New System.Drawing.Size(150, 30)
.Text = "how to get an even?"
frm.Controls.Add(cb)
End With

End Sub

Private Sub cb_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
MsgBox("This doesnt work!")
End Sub

End Class
 
Hello, Try this and good luck! You will also want to do a RemoveHandler in your form close event or where ever you delete the control. It is the exact same syntax except you use RemoveHandler instead of AddHandler.
Code:
With cb
    .Location = New System.Drawing.Point(50, 50)
    .Size = New System.Drawing.Size(150, 30)
    .Text = "how to get an even?"
    frm.Controls.Add(cb)
    AddHandler .Click, AddressOf cb_Click
End With
 
Hello SBendBuckeye,

We got the event working, created code:


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim bt As New Button
AddHandler bt.Click, AddressOf click_evnt
bt.Size = New System.Drawing.Size(60, 20)
bt.Text = "press"
bt.Location = New System.Drawing.Point(50, 50)
Me.Controls.Add(bt)
End Sub

Private Sub click_evnt(ByVal sender As System.Object, ByVal e As System.EventArgs)
MsgBox("ok")
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top