473,320 Members | 1,572 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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

Nov 29 '05 #1
12 814
"Mahesh Devjibhai Dhola [MVP]" <dh*********@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/>

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*********@hotmail.com> wrote in message news:uL**************@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

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*********@hotmail.com> wrote in message
news:uL**************@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
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******************@TK2MSFTNGP09.phx.gbl...
"Mahesh Devjibhai Dhola [MVP]" <dh*********@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/>

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**************@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]" <dh*********@hotmail.com> wrote in message
news:uL**************@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

Nov 30 '05 #6
"Mahesh Devjibhai Dhola [MVP]" <dh*********@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/>

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*********@hotmail.com> a écrit dans le
message de news:O0*************@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" <ne****@nospam.nospam> wrote in message
news:Oi**************@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]" <dh*********@hotmail.com> wrote in message news:uL**************@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


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(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.
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 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

Nov 30 '05 #10
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.

<jm*****@gmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.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

Dec 1 '05 #11

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

Dec 1 '05 #12
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

Dec 1 '05 #13

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

Similar topics

4
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...
3
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...
12
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);...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.