MULTIPLE SENDERS FOR AN EVENT

  • Thread starter Thread starter Rob
  • Start date Start date
R

Rob

All of my controls are built at runtime. The events for
the controls are hard coded and I relate the control to
the event at runtime since the control is created at
runtime. The problem is the same event might be used by
different controls and I am having problems with
determining what control called the event. The Name
member is not supported in in .NET Compact Frameworks so
I can't cast the sender parameter to Object, Control,
Button, etc to find value of the Name member.
What is another way for me to determine what control
called the event.
Thanks for any help offered.
 
You need to use sender parameter:

VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click, Button2.Click

If sender Is Button1 Then

MessageBox.Show("Button1")

End If

If sender Is Button2 Then

MessageBox.Show("Button2")

End If

End Sub


C#:

if ( sender is button1 )
{
MessageBox.Show("Button1");
}
else if ( sender is button2 )
{
MessageBox.Show("Button2");
}
 
Thank you for your reply. I do get what you are saying.
This is my test code:

Private Sub btn1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btn1.Click, btn2.Click

Dim fFlag1 As Boolean
Dim fFlag2 As Boolean

fFlag1 = False
fFlag2 = False

If CType(sender, Button) Is btn1 Then
fFlag1 = True
End If
If CType(sender, Button) Is btn2 Then
fFlag2 = True
End If

End Sub

It does properly route depending on which button was
selected.

The problem is I could have 100 buttons all conntected the
the same event and all of those buttons are created at
runtime not at design time in the development environment.
I can not hard code the conditional with the button names
since I do not know what they will be. I have to
programmically compare the button names like from
sender.Name, except Name in System.Windows.Forms.Button is
not supported in the Compact Framework.
The direction that I am going is to base all my controls
off of a custom control (created from
System.Windows.Froms.Controls) and add a Name member to my
custom control so that I can say sender.Name and get the
Name of the control that invoked the event.
If you have additional thoughts please post them as I am
not sure that my idea is going to work and I need this
solved very soon.

Rob
 
Rob said:
The direction that I am going is to base all my controls
off of a custom control (created from
System.Windows.Froms.Controls) and add a Name member to my
custom control

probably easier to just define an interface and extend each control you need
to implement that interface

then you can just cast the sender to the interface and access the property

Public Class button2
Inherits Button
Implements extendedcontrol
Private name As String
Property ControlName() As String Implements extendedcontrol.ControlName
Get
Return name
End Get
Set(ByVal Value As String)
name = Value
End Set
End Property
End Class

Public Interface extendedcontrol
Property ControlName() As String
End Interface
 
Back
Top