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
21 1354
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.
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
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:la***@vk arlsen.no
PGP KeyID: 0x2A42A1C2
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.Diagnost ics somwhere?
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.Diagnost ics somwhere?
There are the System.Diagnost ics.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
Stefan Simek wrote: There are the System.Diagnost ics.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) :-)
>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.
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******** *************@g 14g2000cwa.goog legroups.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 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
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.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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 the use
of closure could it be done better. But at the moment i am quite
confused and hope that...
|
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 access is granted
|
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)
{
.......
}
|
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,...
|
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.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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,...
|
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...
|
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 into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |