generic event handler

HarryJ

New member
Joined
Apr 12, 2006
Messages
1
Programming Experience
10+
Hi,
Have Form1 with Button1, Button2, and TextBox1. Trying to understand how to create and use my own event handler to determine which button was clicked and display a message in the textbox. Think I'm in the ball park with this solution, but not sure how to code the ButtonClick class.
Thanks,
HarryJ

Public
Class Form1
Inherits System.Windows.Forms.Form
#
Region " Windows Form Designer generated code "

'object to handle BtClked event
Dim WithEvents BtClked As New ButtonClick

Private Sub Button1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Button1.Click
BtClked.Click()
End Sub

Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
BtClked.Click()
End Sub

'generic click event handler
Private Sub Generic_Click(ByVal sender As System.Object) _
Handles BtClked.BtClk
If sender Is Button1 Then
TextBox1.Text = "Button 1 was clicked"
Else
TextBox1.Text = "Button 2 was clicked"
End If
End Sub
End
Class

Public
Class ButtonClick
Public Event BtClk(ByVal Obj As Object) 'new event
Public Sub Click() 'Click method
RaiseEvent BtClk()
End Sub
End
Class
 
I'm not at my pc right now so sorry if the code is a bit iffy. Here's how i would do it...

in the load event...

VB.NET:
Addhandler Button1.click addressof me.generic_click
addhandler Button2.click addressof me.generic_click
Then in the Generic_click sub

VB.NET:
Dim Btn as button = ctype(sender,button)
 
textbox1.text = btn.name + " Was clicked"
 
Back
Top