This is a multi-part message in MIME format.

------=_NextPart_000_0021_01C5F52E.1C9DEDE0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Hi,
I have added few of the events in some control, example code is:
btnControl.GotFocus +=3Dnew EventHandler(EventHandlingMethod);
btnControl.Click +=3Dnew EventHandler(EventHandlingMethod);
lblControl.Click +=3Dnew EventHandler(EventHandlingMethod);

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

btnControl =3D Button object, lblControl =3D Lable object

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

now in EventHandlingMethod 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

------=_NextPart_000_0021_01C5F52E.1C9DEDE0
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.2800.1522" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY>
<DIV><FONT face=3DArial size=3D2>Hi,</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>I have added few of the events in some =
control,=20
example code is:</FONT></DIV>
<DIV>
<P><FONT face=3DArial color=3D#0000ff size=3D2><EM>btnControl.GotFocus =
+=3Dnew=20
EventHandler(EventHandlingMethod);<BR>btnControl.Click +=3Dnew=20
EventHandler(EventHandlingMethod);<BR>lblControl.Click +=3Dnew=20
EventHandler(EventHandlingMethod);</EM></FONT></P>
<P><FONT face=3DArial color=3D#0000ff size=3D2><EM>private void=20
EventHandlingMethod(object sender, EventArgs =
e)<BR>{<BR>&nbsp;&nbsp;&nbsp;=20
.......<BR>}</EM></FONT></P>
<P><EM><FONT face=3DArial color=3D#0000ff size=3D2>btnControl =3D Button =
object,=20
lblControl =3D Lable object</FONT></EM></P>
<P><FONT face=3DArial size=3D2><EM><FONT color=3D#0000ff>Now</FONT></EM> =
the problem=20
is: I am adding the same event handler method in all events for =
btnControl and=20
lblControl too.</FONT></P>
<P><FONT face=3DArial size=3D2>now in EventHandlingMethod method, i can =
find which=20
control is using sender but now if i want to put some switch cases in =
the method=20
for Event Name then I have no any such information in the eventargs or =
anyother=20
way....</FONT></P>
<P><FONT face=3DArial size=3D2>Can someone help/guide me to get the =
event=20
name?</FONT></P>
<P><FONT face=3DArial size=3D2>Is it possible? if yes&nbsp;then how? If =
no, then any=20
work around?</FONT></P>
<P><FONT face=3DArial size=3D2>Thanks in Advance</FONT></P>
<P><FONT face=3DArial size=3D2>Regards,<BR>Mahesh Devjibhai=20
Dhola</FONT></P></DIV></BODY></HTML>

------=_NextPart_000_0021_01C5F52E.1C9DEDE0--

Re: EventHandling problem : Java has solution but what about Microsoft??? by Larry

Larry
Tue Nov 29 10:13:42 CST 2005

(some newsgroups trimmed - this really wasn't appropriate for all the
listed groups)

Mahesh Devjibhai Dhola [MVP] wrote:
> 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)
> {
> .......
> }
>
> 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 EventHandlingMethod 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?

Your design is bad. Methods should do one thing. If you actually want
the same thing to happen on GotFocus and on Click, fine. If you want
*different* things to happen, use *different* procedures. If there is
some common and some different functionality, put the common
functionality in a separate procedure called from both event handlers.

You shouldn't treat event handlers like a substitute for a Windows
message loop, that has to handle everything that can possible happen.
The GotFocus event handler should do what is required on GotFocus; the
Click event handler should do what is required on Click; and so on.

--
Larry Lard
Replies to group please


Re: EventHandling problem : Java has solution but what about Microsoft??? by Herfried

Herfried
Tue Nov 29 10:11:46 CST 2005

"Mahesh Devjibhai Dhola [MVP]" <dholamahesh@hotmail.com> schrieb:
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)
{
.......
}

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 EventHandlingMethod 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/>


Re: EventHandling problem : Java has solution but what about Microsoft??? by Marina

Marina
Tue Nov 29 10:14:05 CST 2005

This is a multi-part message in MIME format.

------=_NextPart_000_0142_01C5F4D6.02B2FB30
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

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]" <dholamahesh@hotmail.com> wrote in =
message news:uL$KN7P9FHA.2832@TK2MSFTNGP14.phx.gbl...
Hi,
I have added few of the events in some control, example code is:
btnControl.GotFocus +=3Dnew EventHandler(EventHandlingMethod);
btnControl.Click +=3Dnew EventHandler(EventHandlingMethod);
lblControl.Click +=3Dnew EventHandler(EventHandlingMethod);

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

btnControl =3D Button object, lblControl =3D Lable object

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

now in EventHandlingMethod 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

------=_NextPart_000_0142_01C5F4D6.02B2FB30
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.2900.2769" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3DArial size=3D2>If you need to do different things =
depending on the=20
event, why not have different event handlers? You can place common code =
into a=20
method that both event handlers can call.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>I understand what you are saying, but =
it just=20
doesn't seem like a big deal. It is very easy to work around, and most =
of the=20
time it is not even an issue because you either need different code in =
the=20
handler anyway, or the events have different signatures, =
etc.</FONT></DIV>
<BLOCKQUOTE dir=3Dltr=20
style=3D"PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; =
BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
<DIV>"Mahesh Devjibhai Dhola [MVP]" &lt;<A=20
=
href=3D"mailto:dholamahesh@hotmail.com">dholamahesh@hotmail.com</A>&gt; =
wrote in=20
message <A=20
=
href=3D"news:uL$KN7P9FHA.2832@TK2MSFTNGP14.phx.gbl">news:uL$KN7P9FHA.2832=
@TK2MSFTNGP14.phx.gbl</A>...</DIV>
<DIV><FONT face=3DArial size=3D2>Hi,</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>I have added few of the events in =
some control,=20
example code is:</FONT></DIV>
<DIV>
<P><FONT face=3DArial color=3D#0000ff size=3D2><EM>btnControl.GotFocus =
+=3Dnew=20
EventHandler(EventHandlingMethod);<BR>btnControl.Click +=3Dnew=20
EventHandler(EventHandlingMethod);<BR>lblControl.Click +=3Dnew=20
EventHandler(EventHandlingMethod);</EM></FONT></P>
<P><FONT face=3DArial color=3D#0000ff size=3D2><EM>private void=20
EventHandlingMethod(object sender, EventArgs =
e)<BR>{<BR>&nbsp;&nbsp;&nbsp;=20
.......<BR>}</EM></FONT></P>
<P><EM><FONT face=3DArial color=3D#0000ff size=3D2>btnControl =3D =
Button object,=20
lblControl =3D Lable object</FONT></EM></P>
<P><FONT face=3DArial size=3D2><EM><FONT =
color=3D#0000ff>Now</FONT></EM> the problem=20
is: I am adding the same event handler method in all events for =
btnControl and=20
lblControl too.</FONT></P>
<P><FONT face=3DArial size=3D2>now in EventHandlingMethod method, i =
can find which=20
control is using sender but now if i want to put some switch cases in =
the=20
method for Event Name then I have no any such information in the =
eventargs or=20
anyother way....</FONT></P>
<P><FONT face=3DArial size=3D2>Can someone help/guide me to get the =
event=20
name?</FONT></P>
<P><FONT face=3DArial size=3D2>Is it possible? if yes&nbsp;then how? =
If no, then=20
any work around?</FONT></P>
<P><FONT face=3DArial size=3D2>Thanks in Advance</FONT></P>
<P><FONT face=3DArial size=3D2>Regards,<BR>Mahesh Devjibhai=20
Dhola</FONT></P></DIV></BLOCKQUOTE></BODY></HTML>

------=_NextPart_000_0142_01C5F4D6.02B2FB30--


Re: EventHandling problem : Java has solution but what about Microsoft??? by Andrew

Andrew
Tue Nov 29 12:13:50 CST 2005

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]" <dholamahesh@hotmail.com> wrote in message
news:uL$KN7P9FHA.2832@TK2MSFTNGP14.phx.gbl...
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)
{
.......
}
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 EventHandlingMethod 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



Re: EventHandling problem : Java has solution but what about Microsoft??? by Mahesh

Mahesh
Wed Nov 30 01:06:31 CST 2005

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]" <hirf-spam-me-here@gmx.at> wrote in message
news:%23NKhX%23P9FHA.1276@TK2MSFTNGP09.phx.gbl...
> "Mahesh Devjibhai Dhola [MVP]" <dholamahesh@hotmail.com> schrieb:
> 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)
> {
> .......
> }
>
> 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 EventHandlingMethod 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/>
>



Re: EventHandling problem : Java has solution but what about Microsoft??? by Mahesh

Mahesh
Wed Nov 30 01:10:16 CST 2005

Dear,
Your design is good or bad, it depends on requirement.
I know what is good and bad design for my situation. I am creating my own
framework and thats why i need such help.
This is very simple and general need to have "Event name" in some cases. I
am surprised that all Event has common property and thats Event name and
EventArgs is simply derived from Object class, instead it should have
atleast name property.
So, please dont concentrate on design and if possible, help me to find out
the answer. That is my need and anyhow i want to solve it.

Thanking you all for your kind support.

"Larry Lard" <larrylard@hotmail.com> wrote in message
news:1133280822.769231.264850@z14g2000cwz.googlegroups.com...
> (some newsgroups trimmed - this really wasn't appropriate for all the
> listed groups)
>
> Mahesh Devjibhai Dhola [MVP] wrote:
> > 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)
> > {
> > .......
> > }
> >
> > 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 EventHandlingMethod 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?
>
> Your design is bad. Methods should do one thing. If you actually want
> the same thing to happen on GotFocus and on Click, fine. If you want
> *different* things to happen, use *different* procedures. If there is
> some common and some different functionality, put the common
> functionality in a separate procedure called from both event handlers.
>
> You shouldn't treat event handlers like a substitute for a Windows
> message loop, that has to handle everything that can possible happen.
> The GotFocus event handler should do what is required on GotFocus; the
> Click event handler should do what is required on Click; and so on.
>
> --
> Larry Lard
> Replies to group please
>



Re: EventHandling problem : Java has solution but what about Microsoft??? by Mahesh

Mahesh
Wed Nov 30 01:14:16 CST 2005

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" <nemoby@nospam.nospam> wrote in message
news:Oi$UnCR9FHA.3660@TK2MSFTNGP09.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]" <dholamahesh@hotmail.com> wrote in message
> news:uL$KN7P9FHA.2832@TK2MSFTNGP14.phx.gbl...
> 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)
> {
> .......
> }
> 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 EventHandlingMethod 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
>
>



Re: EventHandling problem : Java has solution but what about Microsoft??? by Herfried

Herfried
Wed Nov 30 04:38:25 CST 2005

"Mahesh Devjibhai Dhola [MVP]" <dholamahesh@hotmail.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/>


Re: EventHandling problem : Java has solution but what about Microsoft??? by Patrice

Patrice
Wed Nov 30 06:23:13 CST 2005

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]" <dholamahesh@hotmail.com> a écrit dans le
message de news:O0Ro2xX9FHA.912@TK2MSFTNGP10.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" <nemoby@nospam.nospam> wrote in message
> news:Oi$UnCR9FHA.3660@TK2MSFTNGP09.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]" <dholamahesh@hotmail.com> wrote in
message
> > news:uL$KN7P9FHA.2832@TK2MSFTNGP14.phx.gbl...
> > 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)
> > {
> > .......
> > }
> > 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 EventHandlingMethod 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
> >
> >
>
>



Re: EventHandling problem : Java has solution but what about Microsoft??? by Danny

Danny
Wed Nov 30 06:28:32 CST 2005

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(object sender, string command, EventArgs e)
{
// your code here
}

and hook them up like this:

void button1_click(object sender, EventArgs e)
{
GlobalHandler(sender, "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.

Re: EventHandling problem : Java has solution but what about Microsoft??? by jmcgrew

jmcgrew
Wed Nov 30 07:02:21 CST 2005

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 EventHandlingMethod(string eventName, object sender,
EventArgs e)
{
...
}

btnControl.GotFocus += delegate(object s, EventArgs e) {
EventHandlingMethod("GotFocus", s, e); };
btnControl.Click += delegate(object s, EventArgs e) {
EventHandlingMethod("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 EventHandlingMethod(object sender, EventArgs e)
{
string name;
NamedEventArgs nea = e as NamedEventArgs;
if (nea != null) {
name = nea.Name;
e = nea.Inner;
} else {
name = "???";
}
...
}

btnControl.GotFocus += delegate(object s, EventArgs e) {
EventHandlingMethod(s, new NamedEventArgs("GotFocus", e)); };
btnControl.Click += delegate(object s, EventArgs e) {
EventHandlingMethod(s, new NamedEventArgs("Click", e)); };


Jesse


Re: EventHandling problem : Java has solution but what about Microsoft??? by lasse

lasse
Wed Nov 30 08:45:29 CST 2005

Mahesh Devjibhai Dhola [MVP] wrote:
> Dear,
> Your design is good or bad, it depends on requirement.
<snip>

If the "good" design needs an ugly hack to implement, it's not a good
design, regardless of how possible it is in other frameworks.

In this case your only option that I know of would be to access the
stack trace of the methods that called you and look at the method names
there. Ie. if you find "OnClick", it's a click event, and so on.

This is an ugly hack, and depending on access permissions the software
is to run under might not be possible anyway.

The data that is sent to the .NET event handlers carry no information
about what event was triggered that ended up in this method call.

I see this case as designing that cars can fly, calling it a good design
and then later on struggle to figure out how on earth this would be
possible.

It isn't.

Change your design.

--
Lasse Vågsæther Karlsen
http://usinglvkblog.blogspot.com/
mailto:lasse@vkarlsen.no
PGP KeyID: 0x2A42A1C2

Re: EventHandling problem : Java has solution but what about Microsoft??? by Danny

Danny
Wed Nov 30 12:05:19 CST 2005

Lasse Vågsæther Karlsen wrote:
> In this case your only option that I know of would be to access the
> stack trace of the methods that called you and look at the method names
> there. Ie. if you find "OnClick", it's a click event, and so on.

A bit off-topic, but is it possible to programatically get a stack
trace? I've had to do it once (in a test app, so not important how ugly
it was), and resorted to throwing an exception just to get the
StackTrace. I assume there's a nicer away - probably in
System.Diagnostics somwhere?

Re: EventHandling problem : Java has solution but what about Microsoft??? by Stefan

Stefan
Thu Dec 01 02:04:17 CST 2005

Danny Tuppeny wrote:
> Lasse Vågsæther Karlsen wrote:
>> In this case your only option that I know of would be to access the
>> stack trace of the methods that called you and look at the method
>> names there. Ie. if you find "OnClick", it's a click event, and so on.
>
> A bit off-topic, but is it possible to programatically get a stack
> trace? I've had to do it once (in a test app, so not important how ugly
> it was), and resorted to throwing an exception just to get the
> StackTrace. I assume there's a nicer away - probably in
> System.Diagnostics somwhere?

There are the System.Diagnostics.StackTrace/StackFrame classes, but
using them is the ugliest hack I can possibly imagine. And it would not
help at all, because you would only get the name of method that fired
the event (in the better case, might be as well the optimizer inlines
the method and you get the method that called the event-firing method),
which, unless you use the OnXxx convention would not help at all. And,
of course, I expect it to be terribly slow.

Just my 2c.

Stefan

Re: EventHandling problem : Java has solution but what about Microsoft??? by Danny

Danny
Thu Dec 01 02:31:06 CST 2005

Stefan Simek wrote:
> There are the System.Diagnostics.StackTrace/StackFrame classes, but
> using them is the ugliest hack I can possibly imagine. And it would not
> help at all, because you would only get the name of method that fired
> the event (in the better case, might be as well the optimizer inlines
> the method and you get the method that called the event-firing method),
> which, unless you use the OnXxx convention would not help at all. And,
> of course, I expect it to be terribly slow.
>
> Just my 2c.

Cool. Don't worry, I'm not planning on using it in any "proper"
software, but it might come in handy when writing apps to test apps (if
a test fails, an exception might not be thrown, but it'd be nice to have
the route through the code) :-)

Re: EventHandling problem : Java has solution but what about Microsoft??? by Truong

Truong
Thu Dec 01 02:50:35 CST 2005

>Don't worry, I'm not planning on using it in any "proper"
>software
It could be useful in some "proper" software. For example, you write a
logging framework that allows outputting of the method/class name in
the logged message. You could use the Stack to get the name of the
method that call the log routine. Without Stack, I don't know any other
way to get the MethodBase representing the caller method.


Re: EventHandling problem : Java has solution but what about Microsoft??? by Mahesh

Mahesh
Thu Dec 01 06:15:06 CST 2005

Thanks dude,
I am not using .Net 2.0 currently in production environment but i will keep
your solution in my mind.

Thanks for the concern.

<jmcgrew@gmail.com> wrote in message
news:1133355741.138208.30030@g14g2000cwa.googlegroups.com...
> 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 EventHandlingMethod(string eventName, object sender,
> EventArgs e)
> {
> ...
> }
>
> btnControl.GotFocus += delegate(object s, EventArgs e) {
> EventHandlingMethod("GotFocus", s, e); };
> btnControl.Click += delegate(object s, EventArgs e) {
> EventHandlingMethod("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 EventHandlingMethod(object sender, EventArgs e)
> {
> string name;
> NamedEventArgs nea = e as NamedEventArgs;
> if (nea != null) {
> name = nea.Name;
> e = nea.Inner;
> } else {
> name = "???";
> }
> ...
> }
>
> btnControl.GotFocus += delegate(object s, EventArgs e) {
> EventHandlingMethod(s, new NamedEventArgs("GotFocus", e)); };
> btnControl.Click += delegate(object s, EventArgs e) {
> EventHandlingMethod(s, new NamedEventArgs("Click", e)); };
>
>
> Jesse
>



Re: EventHandling problem : Java has solution but what about Microsoft??? by John

John
Thu Dec 01 11:50:34 CST 2005


You should be looking at multicast delegates as an interface for your
events.


Mahesh Devjibhai Dhola [MVP] wrote:
> 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)
> {
> .......
> }/
>
> /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 EventHandlingMethod 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
>

RE: EventHandling problem : Java has solution but what about Microsoft by tristan49

tristan49
Thu Dec 01 15:40:02 CST 2005

Either inherit the controls into a class and override their OnEvent methods
or just put this information in the Control.Tag value... each Control has a
Tag property of type object. However I think having a catch all event handler
is not the solution, but either inheritance of the controls or using the
control tag will work for you.

Chris

"Mahesh Devjibhai Dhola [MVP]" wrote:

> 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)
> {
> .......
> }
>
> 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 EventHandlingMethod 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
>

Re: EventHandling problem : Java has solution but what about Microsoft??? by Stefan

Stefan
Fri Dec 02 02:41:02 CST 2005

Truong Hong Thi wrote:
>> Don't worry, I'm not planning on using it in any "proper"
>> software
> It could be useful in some "proper" software. For example, you write a
> logging framework that allows outputting of the method/class name in
> the logged message. You could use the Stack to get the name of the
> method that call the log routine. Without Stack, I don't know any other
> way to get the MethodBase representing the caller method.
>

Maybe, but there's still the problem I have mentioned in my previous
post. If your calling method gets inlined (I agree it won't happen too
often, at least for non-trivial methods, but who knows how the JIT will
behave in future versions?), you get the caller of the calling method
which can be misleading.

Re: EventHandling problem : Java has solution but what about Microsoft??? by Truong

Truong
Fri Dec 02 04:10:02 CST 2005

I don't know how log4net handles this, but I guess it uses the Stack
also.
I have no idea about the rules compiler uses to decide to inline a
function, but the logging routines is in a separate class in a separate
assembly. Does the runtime ever decide to inline such methods?

Thi


Re: EventHandling problem : Java has solution but what about Microsoft??? by Stefan

Stefan
Fri Dec 02 04:21:03 CST 2005

Truong Hong Thi wrote:
> I don't know how log4net handles this, but I guess it uses the Stack
> also.
> I have no idea about the rules compiler uses to decide to inline a
> function, but the logging routines is in a separate class in a separate
> assembly. Does the runtime ever decide to inline such methods?
>
> Thi
>

AFAIK, the JIT currently inlines methods that have 32 or less IL bytes.
As for the problem, I think you don't need to worry about it, as it's
mostly theoretical. It can happen like this:

using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;

namespace Test
{
class Program
{
static void Main(string[] args)
{
Method1();
}

static void Method1()
{
Method2();
}

static void Method2()
{
WriteCallee();
}

[MethodImpl(MethodImplOptions.NoInlining)]
static void WriteCallee();
{
Console.WriteLine(new StackFrame(1).GetMethod().Name);
}
}
}

in debug mode (inlining disabled) the output is Method2, whereas in
release mode the output is Main

HTH,
Stefan