473,503 Members | 1,747 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

MULTIPLE SENDERS FOR AN EVENT

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

Jul 21 '05 #1
7 1586
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
}

"Rob" <an*******@discussions.microsoft.com> wrote in message
news:00****************************@phx.gbl...
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

Jul 21 '05 #2
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:
Private Sub btn1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btn1.Click

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

"Matt Burland" <an*******@discussions.microsoft.com> wrote in message
news:uU**************@TK2MSFTNGP12.phx.gbl... 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
}

"Rob" <an*******@discussions.microsoft.com> wrote in message
news:00****************************@phx.gbl...
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


Jul 21 '05 #3
Rob
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?
-----Original Message-----
Why are you passing the sender ByVal? It should be passed ByRef otherwiseyou 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 (byonly if you passed it ByRef):

if (sender == mybutn1)
{
// process mybutn1
}
elseif (sender == mybutn2)
{
// process mybutn2
}

"Rob" <an*******@discussions.microsoft.com> wrote in messagenews:00****************************@phx.gbl...
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

.

Jul 21 '05 #4
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
Private Sub Button_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs)
Dim btn As Button = DirectCast(sender, Button) Select Case btn.Name
Case "btn1"
Case "btn2"
End Select End Sub

Hope this helps
Jay

"Rob" <an*******@discussions.microsoft.com> wrote in message
news:03****************************@phx.gbl...
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?
-----Original Message-----
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
}

"Rob" <an*******@discussions.microsoft.com> wrote in

message
news:00****************************@phx.gbl...
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

.

Jul 21 '05 #5
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? : /

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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:
Private Sub btn1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btn1.Click 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

"Matt Burland" <an*******@discussions.microsoft.com> wrote in message
news:uU**************@TK2MSFTNGP12.phx.gbl...
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
}

"Rob" <an*******@discussions.microsoft.com> wrote in message
news:00****************************@phx.gbl...
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



Jul 21 '05 #6
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?

"Rob" <an*******@discussions.microsoft.com> wrote in message
news:03****************************@phx.gbl...
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?
-----Original Message-----
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
}

"Rob" <an*******@discussions.microsoft.com> wrote in

message
news:00****************************@phx.gbl...
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

.

Jul 21 '05 #7
Rob
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.
-----Original Message-----
What you originally had works for me (in C# at least), if I do this in anevent:

Control myctrl = (Control) sender;

MessageBox.Show(myctrl.Name);

I get the name of the control that sent the message and I pretty sure thisis what you were trying to do in VB. It should work so I wonder if thereisn't something else wrong. Are you certain that the event is actuallygetting fired? If you controls are dynamic, are you sure they are actuallygetting names set? How are you creating your controls? What are you actuallygetting when you try and get the name from the sender, does it return nullor throw an exception?

"Rob" <an*******@discussions.microsoft.com> wrote in messagenews:03****************************@phx.gbl...
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?
>-----Original Message-----
>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
>}
>
>"Rob" <an*******@discussions.microsoft.com> wrote in

message
>news:00****************************@phx.gbl...
>> 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
>>
>
>
>.
>

.

Jul 21 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
7169
by: Filips Benoit | last post by:
Dear All, How can i email from access and hide or change the senders email-address? Why: Quotations for a house are made by several salers with an Access-interface on the firms server (...
4
10847
by: Ken Madden | last post by:
I want to create multiple fileSystemWatchers in a Windows Service to constantly watch multiple folders for file creations and then execute certain code based on these actions. The problem is that...
6
4960
by: James Radke | last post by:
Hello, I have a multithreaded windows NT service application (vb.net 2003) that I am working on (my first one), which reads a message queue and creates multiple threads to perform the processing...
7
266
by: Rob | last post by:
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...
4
1712
by: Pascal Schmidt-Volkmar | last post by:
Hi there, in my calculator app the number buttons have (of course) almost the same functionality. Therefore, I would like to store their value in the tag number and read that tag number when any of...
17
1799
by: John | last post by:
Hi I need to package one of my access apps and send to clients. My app calls outlook to send emails. The problem is that client pcs can each have a different version of outlook (2000, xp, 2003...
0
1315
by: Sebastian Loncar | last post by:
Hi, i have two applications, which communicates very extrem together. With the IPC-Channel i receive often the messages like "all instances of the requested pipe are busy". So i want to use...
9
3164
by: Gummy | last post by:
Hello, I created a user control that has a ListBox and a RadioButtonList (and other stuff). The idea is that I put the user control on the ASPX page multiple times and each user control will...
2
1378
by: ashutosh11 | last post by:
Hi all, can anyone please help I am a new to VBA and need help here. I am trying to store EMAILs depending on the senders Email Domain. I should write a VBA script that will run everytime a new...
0
7203
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7087
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7334
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7462
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5014
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3156
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
737
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
383
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.