Event Handler

C

Crumpet

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(); // e or sender - which do I use
and how ?? THANKS
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
 
L

Larry Lard

Crumpet said:
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(); // e or sender - which do I use
and how ?? THANKS
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?

You are so very close to having this solved yourself.

The key is to recognise that with an EventHandler, the 'sender'
parameter - though it is *typed* as an object - *is* the control that
the event pertains to. It is typed as an object so that the signature of
EventHandler can be widely reused, but if you know what it is then you
can cast it and look inside it.

Thus if you have hooked up this method to all the MenuItems' Click
events (which it sounds like you have successfully done already), you
know that whenever miClick is called, sender *will be* a MenuItem. This
sure knowledge allows you to safely write a cast and get all the info
you want:

private void miClick(object sender, EventArgs e)
{
// Because miClick is only hooked up to MenuItem events,
// we can safely:
MenuItem clicked = (MenuItem) sender;

// now we have compile time access to all the members of MenuItem

}

Also, an EventArgs is a baseclass for all eventhandlers to extend and
put their information in (eg MouseEventArgs have mouse position and
button information) - I don't think a plain EventArgs from a Click event
will have anything useful in.
 
C

Crumpet

Thanks - very interesting.

| Crumpet wrote:
| > 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(); // e or sender - which do I
use
| > and how ?? THANKS
| > 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?
|
| You are so very close to having this solved yourself.
|
| The key is to recognise that with an EventHandler, the 'sender'
| parameter - though it is *typed* as an object - *is* the control that
| the event pertains to. It is typed as an object so that the signature of
| EventHandler can be widely reused, but if you know what it is then you
| can cast it and look inside it.
|
| Thus if you have hooked up this method to all the MenuItems' Click
| events (which it sounds like you have successfully done already), you
| know that whenever miClick is called, sender *will be* a MenuItem. This
| sure knowledge allows you to safely write a cast and get all the info
| you want:
|
| private void miClick(object sender, EventArgs e)
| {
| // Because miClick is only hooked up to MenuItem events,
| // we can safely:
| MenuItem clicked = (MenuItem) sender;
|
| // now we have compile time access to all the members of MenuItem
|
| }
|
| Also, an EventArgs is a baseclass for all eventhandlers to extend and
| put their information in (eg MouseEventArgs have mouse position and
| button information) - I don't think a plain EventArgs from a Click event
| will have anything useful in.
|
|
| --
| Larry Lard
| (e-mail address removed)
| The address is real, but unread - please reply to the group
| For VB and C# questions - tell us which version
 

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