Thread: right click manu in c#

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    8

    right click manu in c#

    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,

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    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.

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    8
    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....

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    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.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    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;
            }
        }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Button click out of WM_COMMAND
    By Sober in forum Windows Programming
    Replies: 8
    Last Post: 05-03-2007, 09:07 PM
  2. Replies: 2
    Last Post: 03-23-2006, 07:08 AM
  3. mouse click coords and moving a rect
    By techrolla in forum Windows Programming
    Replies: 1
    Last Post: 03-07-2004, 09:49 PM
  4. Click button on dialog box to open window.
    By ooosawaddee3 in forum Windows Programming
    Replies: 1
    Last Post: 11-29-2002, 08:53 AM
  5. newbie help
    By jcw96 in forum C++ Programming
    Replies: 10
    Last Post: 09-15-2002, 08:10 AM