Answered by:
How to attach events to contextmenustrip

Question
-
Hello,
my question is the following:
I managed to add a Contextmenustrip so when user right-clicks he gets a menu with the option "Delete" and "Exit". The problem is that in the way I created the ContextMenu , I can not differentiate the 2 options therefore I cannot attach them to events!
My code is the followingcontextMenuStrip1.Items.Add("Delete"); contextMenuStrip1.Items.Add("Exit"); contextMenuStrip1.PreviewMouseDown += new MouseButtonEventHandler(contextMenuStrip1_PreviewMouseLeftButtonDown);
To add the contextmenustrip I inserted a form adding the context menu from the toolbox to the left. and then this code you see up, is inserted in the window.cs.
Is any way to solve this or any odifferent way to execute it?
Thanx for your time!Friday, January 15, 2010 11:27 AM
Answers
-
In the tag property of your menuitem save the itemId.
private void AttachContextmenu() { ContextMenu cm = new ContextMenu(); var menuItem = new MenuItem() { Header = "Delete", Tag = "Delete" }; menuItem.PreviewMouseDown += new MouseButtonEventHandler(menuItem_PreviewMouseDown); cm.Items.Add(menuItem); menuItem = new MenuItem() { Header = "Exit", Tag = "Exit" }; menuItem.PreviewMouseDown += new MouseButtonEventHandler(menuItem_PreviewMouseDown); cm.Items.Add(menuItem); myControl.ContextMenu = cm; } void menuItem_PreviewMouseDown(object sender, MouseButtonEventArgs e) { MessageBox.Show(((MenuItem)sender).Tag.ToString()); }
- Proposed as answer by Saraf Talukder Friday, January 15, 2010 2:11 PM
- Marked as answer by penny4yourthoughts Friday, January 15, 2010 4:35 PM
Friday, January 15, 2010 2:10 PM
All replies
-
Hi Penny,Instead of adding the text directly, add menu items and then assign click events for each menu item you will get it.heres some example code which is working.
System.Windows.Controls.ContextMenu cm = new System.Windows.Controls.ContextMenu(); MenuItem mi = new MenuItem(); mi.Header = "Delete"; mi.Click += new RoutedEventHandler(mi_Click); cm.Items.Add(mi); void mi_Click(object sender, RoutedEventArgs e) { throw new NotImplementedException(); }
Hope this is helpful for you.RegardsHiran Repakula.- Proposed as answer by Hiran.Repakula Friday, January 15, 2010 12:31 PM
Friday, January 15, 2010 12:28 PM -
thank you for your answer. I tried this option. I t compiles but on right click instead of menu options ,some dots appear. It is strange.
This is the code I inserted in the Window.cs file
System.Windows.Controls.ContextMenu cm = new System.Windows.Controls.ContextMenu(); cm.Width = 35; MenuItem mi = new MenuItem(); mi.Header = "Delete"; mi.Click += new RoutedEventHandler(mi_Click); cm.Items.Add(mi); } void mi_Click(object sender, RoutedEventArgs e) { MessageBox.Show("hello"); }
Do you have any idea why I cannot see it?Friday, January 15, 2010 1:30 PM -
In the tag property of your menuitem save the itemId.
private void AttachContextmenu() { ContextMenu cm = new ContextMenu(); var menuItem = new MenuItem() { Header = "Delete", Tag = "Delete" }; menuItem.PreviewMouseDown += new MouseButtonEventHandler(menuItem_PreviewMouseDown); cm.Items.Add(menuItem); menuItem = new MenuItem() { Header = "Exit", Tag = "Exit" }; menuItem.PreviewMouseDown += new MouseButtonEventHandler(menuItem_PreviewMouseDown); cm.Items.Add(menuItem); myControl.ContextMenu = cm; } void menuItem_PreviewMouseDown(object sender, MouseButtonEventArgs e) { MessageBox.Show(((MenuItem)sender).Tag.ToString()); }
- Proposed as answer by Saraf Talukder Friday, January 15, 2010 2:11 PM
- Marked as answer by penny4yourthoughts Friday, January 15, 2010 4:35 PM
Friday, January 15, 2010 2:10 PM -
thank you!this did the trick! :)Friday, January 15, 2010 4:35 PM