How to add events to dynamic contextmenu ?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi
I am looking for some help in adding events to dynamic contextmenu.I want to rise the click event for each of the menu ite
that I am creating.

Below is my code for adding dynamic context menu

System.Windows.Forms.MenuItem MnuItem
MnuItem= new System.Windows.Forms.MenuItem()
MnuItem.Text = comboBox1.Text;
cmFavorites.MenuItems.Add(cmFavorites.MenuItems.Count,MnuItem)

can someone help me with the sample lines of code ,how to add the events for the dynamic menu
Thanks for your help in advanc
Priya
 
System.Windows.Forms.MenuItem MnuItem;
MnuItem= new System.Windows.Forms.MenuItem();
MnuItem.Text = comboBox1.Text;

MnuItem.Click += new EventHandler( method_name );

where

void method_name( object sender, EventArgs e )
{
...
}

Regards,
Wiktor Zychla
 
Thanks I am able to get the click event working.One more question in this context
How will I reference the object name inside the click event,since I am creating the context menu dynamically
I cannot reference it using MnuItem.Text any more

System.Windows.Forms.MenuItem MnuItem
MnuItem= new System.Windows.Forms.MenuItem()
MnuItem.Text = comboBox1.Text
cmFavorites.MenuItems.Add(cmFavorites.MenuItems.Count,MnuItem)
MnuItem.Click += new EventHandler( MnuItem_Click )

private void MnuItem_Click( object sender, EventArgs e

//How will I reference MnuItem.Text here.


Thanks for your help
Regards
Priy
 
Thanks I found the solution
For other who is looking for an answer ,here is how it can be done
private void MnuItem_Click( object sender, EventArgs e

MenuItem mi = (MenuItem)sender
MessageBox.Show(mi.Text)
 
Back
Top