hey,
i'm trying for some time now to add a right click manu to a checkedListBox in visual studio c#.
there is no event for a right click...
any suggestions??
thenx ahead,
hey,
i'm trying for some time now to add a right click manu to a checkedListBox in visual studio c#.
there is no event for a right click...
any suggestions??
thenx ahead,
Just assign a context menu to the ContextMenu property of the CheckedListBox:
Code:public Form1() { InitializeComponent(); ContextMenu menu = new ContextMenu(); MenuItem menuItem = new MenuItem("Click me!"); menu.MenuItems.Add(menuItem); checkedListBox1.ContextMenu = menu; }
If you understand what you're doing, you're not learning anything.
tried that, works great, thank you...
but that allows the user to click on anywhere inside the box... i want him to only be able to right click only a checkBox insid the checkedboxlist.
thanx again....
How about using the MouseClick event and checking if the right button was pressed? Then you could see what the SelectedItem is in the listbox. If there isn't one, don't show the menu.
If you understand what you're doing, you're not learning anything.
Use the ContextMenu's Opening event to suppress the menu if no items have been clicked/selected...
Code:public partial class Form1 : Form { public Form1() { InitializeComponent(); this.checkedListBox1.ContextMenuStrip = new ContextMenuStrip(); this.checkedListBox1.ContextMenuStrip.Opening += new CancelEventHandler(this.MenuOpening); this.checkedListBox1.ContextMenuStrip.Items.Add("what is it?"); this.checkedListBox1.ContextMenuStrip.Items[0].Click += new EventHandler(this.MenuOptionClicked); } private void MenuOptionClicked(object sender, EventArgs e) { if (this.checkedListBox1.SelectedIndex > -1) MessageBox.Show("it is " + this.checkedListBox1.SelectedItem); } private void MenuOpening(object sender, CancelEventArgs e) { if (this.checkedListBox1.SelectedIndex == -1) e.Cancel = true; } }