473,396 Members | 2,002 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,396 software developers and data experts.

Event Handler 00026986945

**
[If you saw this post earlier as a reply, please excuse this second post as
my original post was named the same as another thread and got subsumed into
as a reply rather than getting posted as OP]
**
I have a menu with a number of items that I want to handle their being
clicked on with just one handling method.

What I want is for the even handler method to recognize which menu item got
clicked. What is considered the best way to do that?

e.g.

private void miClick(object sender, EventArgs e)
{
string displaystring = e.ToString();
MessageBox.Show(displaystring, "For your information", MessageBoxButtons.OK,
MessageBoxIcon.Information);
}

I can get some specific info but it seems rather convoluted. If I click on
the "About" menu item then the MessageBox will read:
"For your information
System.Windows.Forms.MenuItem, Items.Count: 0, Text: &About

OK"
Do I have to use some sort of regex or is there an easier way to identify
the specific menu item that was click?
Thanks
Aug 17 '06 #1
5 1140
Take a look at the sender; you should be able to cast this back into your
original menu item object, and then access the .Text, .Tag, or whatever else
you want.

Marc
Aug 17 '06 #2
Right on, and I can use a "switch" statement or some such.
"Marc Gravell" <ma**********@gmail.comwrote in message
news:%2***************@TK2MSFTNGP05.phx.gbl...
| Take a look at the sender; you should be able to cast this back into your
| original menu item object, and then access the .Text, .Tag, or whatever
else
| you want.
|
| Marc
|
|
Aug 17 '06 #3
Do you know of documentation that dicusses Control.Tag other than:

http://msdn2.microsoft.com/en-us/lib...ntrol.tag.aspx

??

Thank you for any reply.

"Marc Gravell" <ma**********@gmail.comwrote in message
news:%2***************@TK2MSFTNGP05.phx.gbl...
| Take a look at the sender; you should be able to cast this back into your
| original menu item object, and then access the .Text, .Tag, or whatever
else
| you want.
|
| Marc
|
|
Aug 18 '06 #4
Not doc'n, but the /general/ meaning (sometimes it is more tightly
defined) of .Tag is:

You (the developer) can use .Tag for your own purposes; it is typed as
object, so contain any single thing (which could be an array), but you
will need to track what it contains, as it only your doing. You will
need to cast it back to what you think it is to use it, though...

for instance, you could put a MethodInvoker instance in there so that
each menu-item (from this chain) keeps its "what to do when clicked" in
the .Tag property (for some reason) - then:

// initialization
newItem.Tag = new MethodInvoker(SomeMethodWithoutParams);
// or
newItem.Tag = (MethodInvoker) delegate {DoSomething(newItem, "abc",
123);};

then later (perhaps during the shared Click event handler):

MenuItem mi = sender as MenuItem;
if(mi!=null) { // sender *was* a MenuItem
// some generic checks
// more shared code
MethodInvoker action = mi.Tag as MethodInvoker;
if(mi!=null) mi(); // invoke action if any
}

Alternatively, .Tag is often used to track the business entity of
identity / key that corresponds to the UI element, particularly when a
control is dynamically repeated by the data being displayed - i.e. you
might show a TextBox for each user in a database, and have .Tag contain
the corresponding (app defined) SystemUser class (or something that
will help the developer *find* that instance, such as the username,
even if the .Text is the "friendly name" or "display name").

Or it could contain the original (pre-edit) values. Up to you (the
developer), you see?

Does that help?

Marc

Aug 18 '06 #5
Should have been:
if(action !=null) action (); // invoke action if any

Marc

Aug 18 '06 #6

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

Similar topics

10
by: tony kulik | last post by:
This code works fine in ie and opera but not at all in Mozilla. Anybody got a clue as to how to get it right? <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <script...
18
by: Christopher W. Douglas | last post by:
I am writing a VB.NET application in Visual Studio 2003. I have written a method that handles several events, such as closing a form and changing the visible status of a form. I have some code...
8
by: Mark | last post by:
Hi, I'm looking for some ideas on how to build a very simple Event processing framework in my C++ app. Here is a quick background ... I'm building a multithreaded app in C++ (on Linux) that...
6
by: vbMark | last post by:
If I have a control, for example a CheckedListBox, how do I add and event to code, for example that a box has been checked by the user? Thanks
13
by: Charles Law | last post by:
Mr "yEaH rIgHt" posted the following link about a week ago in answer to my question about removing event handlers. > http://www.vbinfozine.com/t_bindevt.shtml Following on from that post, the...
3
by: Beth | last post by:
in the following: this.ExitButton.Click += new System.EventHandler(this.ExitButton_Click); if I saw an equation, such as y +=x; then y = y+x. But what is the meaning in the event handler. I...
5
by: Richard Grant | last post by:
Hi, I need to "save" in a variable the event handler sub of a control's event, then perform some process, and finally "restore" the originally saved event handler. Example in pseudo-code: 1)...
1
by: tdan | last post by:
I do not know how to get Event.stopObserving() to work in the context I am using it. I am displaying a Color Selection Table and attaching 2 events: 1. onmouseover to display the color to the user...
2
by: John Kotuby | last post by:
Hi guys, I am converting a rather complicated database driven Web application from classic ASP to ASP.NET 2.0 using VB 2005 as the programming language. The original ASP application works quite...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
0
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...
0
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,...

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.