473,657 Members | 2,283 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(ByVa l sender As System.Object,
ByVal e As System.EventArg s) Handles btn1.Click
Dim btnName As String
btnName = CType(sender, Button).Name
End Sub

-- or --

Private Sub btn1_Click(ByVa l sender As System.Object,
ByVal e As System.EventArg s) 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 1601
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*******@disc ussions.microso ft.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(ByVa l sender As System.Object,
ByVal e As System.EventArg s) Handles btn1.Click
Dim btnName As String
btnName = CType(sender, Button).Name
End Sub

-- or --

Private Sub btn1_Click(ByVa l sender As System.Object,
ByVal e As System.EventArg s) 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(ByVa l sender As System.Object,
ByVal e As System.EventArg s) 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*******@disc ussions.microso ft.com> wrote in message
news:uU******** ******@TK2MSFTN GP12.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*******@disc ussions.microso ft.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(ByVa l sender As System.Object,
ByVal e As System.EventArg s) Handles btn1.Click
Dim btnName As String
btnName = CType(sender, Button).Name
End Sub

-- or --

Private Sub btn1_Click(ByVa l sender As System.Object,
ByVal e As System.EventArg s) 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*******@disc ussions.microso ft.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(ByVa l sender As System.Object,
ByVal e As System.EventArg s) Handles btn1.Click
Dim btnName As String
btnName = CType(sender, Button).Name
End Sub

-- or --

Private Sub btn1_Click(ByVa l sender As System.Object,
ByVal e As System.EventArg s) 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(By Val sender As System.Object,
ByVal e As System.EventArg s)
Dim btn As Button = DirectCast(send er, Button) Select Case btn.Name
Case "btn1"
Case "btn2"
End Select End Sub

Hope this helps
Jay

"Rob" <an*******@disc ussions.microso ft.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*******@disc ussions.microso ft.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(ByVa l sender As System.Object,
ByVal e As System.EventArg s) Handles btn1.Click
Dim btnName As String
btnName = CType(sender, Button).Name
End Sub

-- or --

Private Sub btn1_Click(ByVa l sender As System.Object,
ByVal e As System.EventArg s) 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******** ********@TK2MSF TNGP11.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(ByVa l sender As System.Object,
ByVal e As System.EventArg s) 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*******@disc ussions.microso ft.com> wrote in message
news:uU******** ******@TK2MSFTN GP12.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*******@disc ussions.microso ft.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(ByVa l sender As System.Object,
ByVal e As System.EventArg s) Handles btn1.Click
Dim btnName As String
btnName = CType(sender, Button).Name
End Sub

-- or --

Private Sub btn1_Click(ByVa l sender As System.Object,
ByVal e As System.EventArg s) 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*******@disc ussions.microso ft.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*******@disc ussions.microso ft.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(ByVa l sender As System.Object,
ByVal e As System.EventArg s) Handles btn1.Click
Dim btnName As String
btnName = CType(sender, Button).Name
End Sub

-- or --

Private Sub btn1_Click(ByVa l sender As System.Object,
ByVal e As System.EventArg s) 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.Sho w(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*******@disc ussions.microso ft.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*******@disc ussions.microso ft.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(ByVa l sender As System.Object,
>> ByVal e As System.EventArg s) Handles btn1.Click
>> Dim btnName As String
>> btnName = CType(sender, Button).Name
>> End Sub
>>
>> -- or --
>>
>> Private Sub btn1_Click(ByVa l sender As System.Object,
>> ByVal e As System.EventArg s) 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
7181
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 ( Remote Desktop) . When sending the report by email to the client the firm wants the salers email in the from-property and not the servers email.
4
10863
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 these directories are not part of the same directory so I cannot use the subfolder flag. The number of folders also has to be dynamic so I cannot simply create a certain number of FileSystemWatchers with different paths. I am going to pull a...
6
4984
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 for long running reports. When the processing is complete it uses crystal reports to load a template file, populate it, and then export it to a PDF. It works fine so far....
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 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
4
1718
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 those buttons is pressed. But how does this work?? private void button3_Click(object sender, EventArgs e) { if sender.GetType() is button
17
1821
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 etc.). How can I package my app that it will work with any version of outlook? Thanks Regards
0
1333
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 multiple Channels to communicate, maybe 10 Connections. For this scenario i created on each side 10 Servers and 10 Channels, each Channel has a Number in its Name(for example myserver.0 to myserver.9 and myclient.0 to myclient.9)
9
3184
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 load with different data (locations, departments, etc.).
2
1391
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 Email has arrived and if matches with the Email Domain, outlook should store it in a local folder. Thanks and Regards Ash
0
8411
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8323
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8739
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8513
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7351
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6176
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2740
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.