The Click event handler has a sender argument - this will be the control
which generated the event - e.g. menu item:-
// The EventHandler - it fires for all generated MenuItems
private void tmp_Click(object sender, EventArgs e)
{
// How can I distinguish _here_ _which_ MenuItem was hit?
// sender
MenuItem mnusender = (MenuItem)sender;
//switch action based on text of control
switch(ctlsender.Text)
{
case "Open":
//do something here
break;
//etc
}
}
Peter
--
Peter Foot
Windows Embedded MVP
www.inthehand.com |
www.opennetcf.org
"Christian Ahrenkiel" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi!
>
> I've got a submenu in my ContextMenu, which is generated during
> runtime.
> I'm not sure about handling the Click-Event in dynamic menues.
>
> I'm abled to fire the Click-Event for all MenuItems, but cannot
> distinguish which MenuItem was clicked.
>
> This is just an example how I'm handling it (very simplyfied for this
> posting - i hope there are no confusing errors):
>
> // Having an array with strings
> for(int i=0; i < myarray.Length; i++)
> {
> addSubmenu(myarray[i]);
> }
>
> // Each element shall be a MenuItem
> private void addSubmenu(string menu)
> {
> // Creating a new temporarily MenuItem
> MenuItem mniTemp = new MenuItem();
> mniTemp.Text = menu;
> // Adding this MenuItem to my menu
> this.mniMenu.MenuItems.Add(mniTemp);
> // Adding an EventHandler
> mniTemp.Click += new EventHandler(tmp_Click);
> }
>
> // The EventHandler - it fires for all generated MenuItems
> private void tmp_Click(object sender, EventArgs e)
> {
> // How can I distinguish _here_ _which_ MenuItem was hit?
> }
>
> Are there any other possibilities? Maybe with ArrayLists (analogous to
> ArrayLists and Comboboxes)?
>
> Christian