Event Handler 00026986945

C

Crumpet

**
[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
 
M

Marc Gravell

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
 
C

Crumpet

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
|
|
 
M

Marc Gravell

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top