MULTIPLE SENDERS FOR AN EVENT

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

Rob

I need a way to determine the name of the control that
called an event. I have tested many different code
variations but none seem to work. The one that makes the
most sense to me is below, but it does not work either.
The Name property is not valid here but I would expect it
to be. What am I doing wrong.

Private Sub btn1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btn1.Click
Dim btnName As String
btnName = CType(sender, Button).Name
End Sub

-- or --

Private Sub btn1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btn1.Click
Dim btn As Button
Dim btnName As String
btn = CType(sender, Button)
btnName = btn.Name
End Sub

Thanks for any help you can provide me.

Rob
 
Why are you passing the sender ByVal? It should be passed ByRef otherwise
you are making a copy of it.
You should be able to cast sender to Control and read it's Name property.
Failing that you can compare it which ever object you think it might be (by
only if you passed it ByRef):

if (sender == mybutn1)
{
// process mybutn1
}
elseif (sender == mybutn2)
{
// process mybutn2
}
 
Matt,
Why are you passing the sender ByVal?
sender is sent ByVal as its an Object which is a reference type.
It should be passed ByRef otherwise
you are making a copy of it.
ByVal does not make copies of "it"!!!

When you pass a Reference Type ByVal you are passing a copy of the
reference, there is still only a single object on the heap. If you pass a
Value Type (such as integer) ByVal you are passing a copy of the value.

You should only use ByRef when you need a reference to the original variable
(either Value Type or Reference Type), as you intend on changing the
original variable.

Also I hope you realize that:Is the standard .NET convention for event handlers the OP has no real choice
in whether these parameters are ByVal or ByRef as that is defined by
designers of the class raising the event, not the individual using the
class...

Hope this helps
Jay
 
I tested the code that you provided below and it worked as
designed. One problem though, all of my controls are
dynamic and I will not be able to test the sender to a
particular name in code since I will not know what the
name will be. I still need to get the sender control's
name. Do you have any ideas?
 
Rob,
If you are creating the controls dynamically, you will need to assign the
name dynamically, using AddHandler can dynamically add event handlers to the
dynamically created buttons.

In your event handler, you can then check the name property of the control
to see what its name is.

private sub CreateButton()
Dim btn As Button

btn = New Button
btn.Name = "btn1"
AddHandler btn.Click, Button_Click

btn = New Button
btn.Name = "btn2"
AddHandler btn.Click, Button_Click

End Sub
Select Case btn.Name
Case "btn1"
Case "btn2"
End Select
Hope this helps
Jay
 
Whoops, good point. I don't actually use VB and in C# I guess it is implied
that they are passed by value without having to be stated. I'll shut up now
shall I? : /
 
What you originally had works for me (in C# at least), if I do this in an
event:

Control myctrl = (Control) sender;

MessageBox.Show(myctrl.Name);

I get the name of the control that sent the message and I pretty sure this
is what you were trying to do in VB. It should work so I wonder if there
isn't something else wrong. Are you certain that the event is actually
getting fired? If you controls are dynamic, are you sure they are actually
getting names set? How are you creating your controls? What are you actually
getting when you try and get the name from the sender, does it return null
or throw an exception?
 
I haven't implemented the code that creates the controls
dynamically yet. I am still trying to prove that I can get
the whole concept to work. In my test code I do break
inside the event so I know it is firing and there is no
dynamic stuff right now just a form, button, and a click
event. There is no Name property for the Sender in the
IntelliSense dropdown only GetType. When I cast to a
Button or Control using CType, I still do not get the Name
property. In my orignial code example where I had the Name
property listed (btnName = CType(sender, Button).Name),
well that would actually error.. I just typed it in as an
example of what I expected to be right.
Thanks for all of your responses.
 
Back
Top