473,587 Members | 2,524 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

EventHandling problem : Java has solution but what about Microsoft???

Hi,
I have added few of the events in some control, example code is:
btnControl.GotF ocus +=new EventHandler(Ev entHandlingMeth od);
btnControl.Clic k +=new EventHandler(Ev entHandlingMeth od);
lblControl.Clic k +=new EventHandler(Ev entHandlingMeth od);

private void EventHandlingMe thod(object sender, EventArgs e)
{
.......
}

btnControl = Button object, lblControl = Lable object

Now the problem is: I am adding the same event handler method in all events for btnControl and lblControl too.

now in EventHandlingMe thod method, i can find which control is using sender but now if i want to put some switch cases in the method for Event Name then I have no any such information in the eventargs or anyother way....

Can someone help/guide me to get the event name?

Is it possible? if yes then how? If no, then any work around?

Thanks in Advance

Regards,
Mahesh Devjibhai Dhola

Nov 29 '05 #1
12 1273
"Mahesh Devjibhai Dhola [MVP]" <dh*********@ho tmail.com> schrieb:
I have added few of the events in some control, example code is:
btnControl.GotF ocus +=new EventHandler(Ev entHandlingMeth od);
btnControl.Clic k +=new EventHandler(Ev entHandlingMeth od);
lblControl.Clic k +=new EventHandler(Ev entHandlingMeth od);

private void EventHandlingMe thod(object sender, EventArgs e)
{
.......
}

btnControl = Button object, lblControl = Lable object

Now the problem is: I am adding the same event handler method in all events
for btnControl and lblControl too.

now in EventHandlingMe thod method, i can find which control is using sender
but now if i want to put some switch cases in the method for Event Name then
I have no any such information in the eventargs or anyother way....
=======

I am curious which sense handling different events of different controls in
the same event handler would make. Instead, add separate event handlers for
all event types, not just delegate types. This means separate handlers for
controls' 'Click' and 'GotFocus' events.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 29 '05 #2
If you need to do different things depending on the event, why not have different event handlers? You can place common code into a method that both event handlers can call.

I understand what you are saying, but it just doesn't seem like a big deal. It is very easy to work around, and most of the time it is not even an issue because you either need different code in the handler anyway, or the events have different signatures, etc.
"Mahesh Devjibhai Dhola [MVP]" <dh*********@ho tmail.com> wrote in message news:uL******** ******@TK2MSFTN GP14.phx.gbl...
Hi,
I have added few of the events in some control, example code is:
btnControl.GotF ocus +=new EventHandler(Ev entHandlingMeth od);
btnControl.Clic k +=new EventHandler(Ev entHandlingMeth od);
lblControl.Clic k +=new EventHandler(Ev entHandlingMeth od);

private void EventHandlingMe thod(object sender, EventArgs e)
{
.......
}

btnControl = Button object, lblControl = Lable object

Now the problem is: I am adding the same event handler method in all events for btnControl and lblControl too.

now in EventHandlingMe thod method, i can find which control is using sender but now if i want to put some switch cases in the method for Event Name then I have no any such information in the eventargs or anyother way....

Can someone help/guide me to get the event name?

Is it possible? if yes then how? If no, then any work around?

Thanks in Advance

Regards,
Mahesh Devjibhai Dhola

Nov 29 '05 #3
Mahesh ,

I agree with what Herfried and Marina have written with regards to different
controls of different types.

The one place where I do find it useful to use a single event handler for
multiple controls is with Button Controls. Take a look at the Button Command
event. You can pass different CommandName and CommandArgument strings for
each button while using the same Button Command event handler.

This works with traditional Button controls as well as Link Buttons and
Image Buttons.

--

Andrew Robinson
www.binaryocean.com
www.bellinghamdotnet.org
"Mahesh Devjibhai Dhola [MVP]" <dh*********@ho tmail.com> wrote in message
news:uL******** ******@TK2MSFTN GP14.phx.gbl...
Hi,
I have added few of the events in some control, example code is:
btnControl.GotF ocus +=new EventHandler(Ev entHandlingMeth od);
btnControl.Clic k +=new EventHandler(Ev entHandlingMeth od);
lblControl.Clic k +=new EventHandler(Ev entHandlingMeth od);
private void EventHandlingMe thod(object sender, EventArgs e)
{
.......
}
btnControl = Button object, lblControl = Lable object
Now the problem is: I am adding the same event handler method in all events
for btnControl and lblControl too.
now in EventHandlingMe thod method, i can find which control is using sender
but now if i want to put some switch cases in the method for Event Name then
I have no any such information in the eventargs or anyother way....
Can someone help/guide me to get the event name?
Is it possible? if yes then how? If no, then any work around?
Thanks in Advance
Regards,
Mahesh Devjibhai Dhola
Nov 29 '05 #4
Dear,
The Microsoft Event handling framework allows me to do this what you suggest
but some time, its possible to have such requirement so its not the matter
of sense but its my need so if you can suggest me the woraround for my
prolme then it will be helpful to me.

"Herfried K. Wagner [MVP]" <hi************ ***@gmx.at> wrote in message
news:%2******** **********@TK2M SFTNGP09.phx.gb l...
"Mahesh Devjibhai Dhola [MVP]" <dh*********@ho tmail.com> schrieb:
I have added few of the events in some control, example code is:
btnControl.GotF ocus +=new EventHandler(Ev entHandlingMeth od);
btnControl.Clic k +=new EventHandler(Ev entHandlingMeth od);
lblControl.Clic k +=new EventHandler(Ev entHandlingMeth od);

private void EventHandlingMe thod(object sender, EventArgs e)
{
.......
}

btnControl = Button object, lblControl = Lable object

Now the problem is: I am adding the same event handler method in all events for btnControl and lblControl too.

now in EventHandlingMe thod method, i can find which control is using sender but now if i want to put some switch cases in the method for Event Name then I have no any such information in the eventargs or anyother way....
=======

I am curious which sense handling different events of different controls in the same event handler would make. Instead, add separate event handlers for all event types, not just delegate types. This means separate handlers for controls' 'Click' and 'GotFocus' events.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 30 '05 #5
Dear,
I am creating my own framework and its not centric to Buttton or some
control only. It will be applicable to all the events of all the controls.
So i need one common place which will be a common event handler method for
all and from that method my framework will work ahead by checking the sender
control and event name and i am doing lot other things at event time and i
will need more info than just sender and eventargs so i need to have such
method and switch case for event names.
For the same type of situation, java allows to have event name but i am
wondering why Microsoft has not such a simple provision in EventArgs??

"Andrew Robinson" <ne****@nospam. nospam> wrote in message
news:Oi******** ******@TK2MSFTN GP09.phx.gbl...
Mahesh ,

I agree with what Herfried and Marina have written with regards to different controls of different types.

The one place where I do find it useful to use a single event handler for
multiple controls is with Button Controls. Take a look at the Button Command event. You can pass different CommandName and CommandArgument strings for
each button while using the same Button Command event handler.

This works with traditional Button controls as well as Link Buttons and
Image Buttons.

--

Andrew Robinson
www.binaryocean.com
www.bellinghamdotnet.org
"Mahesh Devjibhai Dhola [MVP]" <dh*********@ho tmail.com> wrote in message
news:uL******** ******@TK2MSFTN GP14.phx.gbl...
Hi,
I have added few of the events in some control, example code is:
btnControl.GotF ocus +=new EventHandler(Ev entHandlingMeth od);
btnControl.Clic k +=new EventHandler(Ev entHandlingMeth od);
lblControl.Clic k +=new EventHandler(Ev entHandlingMeth od);
private void EventHandlingMe thod(object sender, EventArgs e)
{
.......
}
btnControl = Button object, lblControl = Lable object
Now the problem is: I am adding the same event handler method in all events for btnControl and lblControl too.
now in EventHandlingMe thod method, i can find which control is using sender but now if i want to put some switch cases in the method for Event Name then I have no any such information in the eventargs or anyother way....
Can someone help/guide me to get the event name?
Is it possible? if yes then how? If no, then any work around?
Thanks in Advance
Regards,
Mahesh Devjibhai Dhola

Nov 30 '05 #6
"Mahesh Devjibhai Dhola [MVP]" <dh*********@ho tmail.com> schrieb:
The Microsoft Event handling framework allows me to do this what you
suggest
but some time, its possible to have such requirement so its not the matter
of sense but its my need so if you can suggest me the woraround for my
prolme then it will be helpful to me.


No, I can't suggest a workaround, because each workaround would be an ugly
hack I would not want to see getting into production code.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 30 '05 #7
EventArgs is just the base class for event arguments. You could perhaps
define your own class that would pass the information you need.

I won't discuss this design but it looks like you could also inherits from
those controls and change the On<Event> method ?

--
Patrice

"Mahesh Devjibhai Dhola [MVP]" <dh*********@ho tmail.com> a écrit dans le
message de news:O0******** *****@TK2MSFTNG P10.phx.gbl...
Dear,
I am creating my own framework and its not centric to Buttton or some
control only. It will be applicable to all the events of all the controls.
So i need one common place which will be a common event handler method for
all and from that method my framework will work ahead by checking the sender control and event name and i am doing lot other things at event time and i
will need more info than just sender and eventargs so i need to have such
method and switch case for event names.
For the same type of situation, java allows to have event name but i am
wondering why Microsoft has not such a simple provision in EventArgs??

"Andrew Robinson" <ne****@nospam. nospam> wrote in message
news:Oi******** ******@TK2MSFTN GP09.phx.gbl...
Mahesh ,

I agree with what Herfried and Marina have written with regards to

different
controls of different types.

The one place where I do find it useful to use a single event handler for multiple controls is with Button Controls. Take a look at the Button

Command
event. You can pass different CommandName and CommandArgument strings for each button while using the same Button Command event handler.

This works with traditional Button controls as well as Link Buttons and
Image Buttons.

--

Andrew Robinson
www.binaryocean.com
www.bellinghamdotnet.org
"Mahesh Devjibhai Dhola [MVP]" <dh*********@ho tmail.com> wrote in message news:uL******** ******@TK2MSFTN GP14.phx.gbl...
Hi,
I have added few of the events in some control, example code is:
btnControl.GotF ocus +=new EventHandler(Ev entHandlingMeth od);
btnControl.Clic k +=new EventHandler(Ev entHandlingMeth od);
lblControl.Clic k +=new EventHandler(Ev entHandlingMeth od);
private void EventHandlingMe thod(object sender, EventArgs e)
{
.......
}
btnControl = Button object, lblControl = Lable object
Now the problem is: I am adding the same event handler method in all

events
for btnControl and lblControl too.
now in EventHandlingMe thod method, i can find which control is using

sender
but now if i want to put some switch cases in the method for Event Name

then
I have no any such information in the eventargs or anyother way....
Can someone help/guide me to get the event name?
Is it possible? if yes then how? If no, then any work around?
Thanks in Advance
Regards,
Mahesh Devjibhai Dhola


Nov 30 '05 #8
Mahesh Devjibhai Dhola [MVP] wrote:
The Microsoft Event handling framework allows me to do this what you suggest
but some time, its possible to have such requirement so its not the matter
of sense but its my need so if you can suggest me the woraround for my
prolme then it will be helpful to me.


It doesn't make sense, but you could create a method called from all
event handlers, eg.

void GlobalHandler(o bject sender, string command, EventArgs e)
{
// your code here
}

and hook them up like this:

void button1_click(o bject sender, EventArgs e)
{
GlobalHandler(s ender, "Click", e);
}

But I agree with the other posts - it's stupid. If you're doing the same
thing, you wouldn't need the string. If you're doing different things,
they should be in seperate methods.
Nov 30 '05 #9
This design seems strange to me too, but I'll take your word that it
makes sense for your project. ;)

This seems like a good time to use C# 2.0's anonymous delegates. If
your event handling method doesn't need to have the standard event
signature, you could do something like this...
private void EventHandlingMe thod(string eventName, object sender,
EventArgs e)
{
...
}

btnControl.GotF ocus += delegate(object s, EventArgs e) {
EventHandlingMe thod("GotFocus" , s, e); };
btnControl.Clic k += delegate(object s, EventArgs e) {
EventHandlingMe thod("Click", s, e); };
Now, if you do need to use the standard event signature, you could
create a new EventArgs class that includes a name, stuff the event name
in there, and pass that in as the e parameter:
class NamedEventArgs : EventArgs {
NamedEventArgs( string name, EventArgs inner) { ... }
public string Name { get ... }
public EventArgs InnerArgs { get ... }
}

private void EventHandlingMe thod(object sender, EventArgs e)
{
string name;
NamedEventArgs nea = e as NamedEventArgs;
if (nea != null) {
name = nea.Name;
e = nea.Inner;
} else {
name = "???";
}
...
}

btnControl.GotF ocus += delegate(object s, EventArgs e) {
EventHandlingMe thod(s, new NamedEventArgs( "GotFocus", e)); };
btnControl.Clic k += delegate(object s, EventArgs e) {
EventHandlingMe thod(s, new NamedEventArgs( "Click", e)); };
Jesse

Nov 30 '05 #10

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

Similar topics

4
2402
by: Marc Tanner | last post by:
Hello, I am currently working on a eventhandling system or something similar, and have the problem of loosing scope. I have read many interesting posts on this group and the faq article about closure, but it seems that i have still not understood everything. Below is my attempt to preserve the scope but it's not really nice and i think with...
3
1182
by: Claes Rådström | last post by:
Hi ! We have a base class that derives from System.Web.UI.Page Alla our pages derives from it. In our base class we want to have an access check, (own) , to verfy user access to the derived page. We want this in the base class so that the programmer cant forget to implement it. => we want a page load in the base class to fire first, if...
21
1326
by: Mahesh Devjibhai Dhola [MVP] | last post by:
Hi, I have added few of the events in some control, example code is: btnControl.GotFocus +=new EventHandler(EventHandlingMethod); btnControl.Click +=new EventHandler(EventHandlingMethod); lblControl.Click +=new EventHandler(EventHandlingMethod); private void EventHandlingMethod(object sender, EventArgs e) { ....... }
0
7843
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...
0
8206
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7967
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...
0
8220
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5392
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3840
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...
0
3875
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2353
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
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.