Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / August 2006

Tip: Looking for answers? Try searching our database.

Event Handler 00026986945

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Crumpet - 17 Aug 2006 16:43 GMT
**
[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
Marc Gravell - 17 Aug 2006 16:51 GMT
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
Crumpet - 17 Aug 2006 20:58 GMT
Right on, and I can use a "switch" statement or some such.

| 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
Crumpet - 18 Aug 2006 01:17 GMT
Do you know of documentation that dicusses Control.Tag other than:

http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.tag.aspx

??

Thank you for any reply.

| 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
Marc Gravell - 18 Aug 2006 06:12 GMT
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
Marc Gravell - 18 Aug 2006 06:13 GMT
Should have been:
 if(action !=null) action (); // invoke action if any

Marc

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.