I have created a context menu that appears when the user right-clicks. I cant figure out how to call a form when one of the menuitems is clicked. see my code below.
if (e.Button == MouseButtons.Right)
{
ContextMenu m = new ContextMenu();
m.MenuItems.Add(new MenuItem("New"));
m.MenuItems.Add(new MenuItem("-"));
m.MenuItems.Add(new MenuItem("Old"));
m.MenuItems.Add(new MenuItem("Lost"));
m.MenuItems.Add(new MenuItem("-"));
m.MenuItems.Add(new MenuItem("Found"));
switch(m.Name)
{
case "New":
new FrmInfo().ShowDialog();
break;
}
You can use MenuItem(String, EventHandler) constructor, it takes OnClick event handler as second argument.
So your code could look like
m.MenuItems.Add(new MenuItem("New", MenuItemNew_Click));
...
private void MenuItemNew_Click(Object sender, System.EventArgs e)
{
new FrmInfo().ShowDialog();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With