Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Don't show context menu if nothing is selected

Tags:

c#

I like to have a context menu only show up if an item is actually selected in a listbox in a winforms c# application.

Currently, I am able to select an item if it is right clicked properly, and I can disable the right click menu if nothing is selected, however, I don't want the menu to even show up.

how can this be accomplished?

private void genPassMenu_Opening(object sender, CancelEventArgs e)
    {
        genPassMenu.Enabled = lstPasswords.SelectedIndex > 0;
        genPassMenu.Visible = lstPasswords.SelectedIndex > 0;

    }

I tried both of those situations on their own, and it only works for enabled.
Perhaps Opening isn't the correct event to choose?
Tx

like image 457
user289130 Avatar asked Mar 09 '10 15:03

user289130


2 Answers

Try this:

private void genPassMenu_Opening(object sender, CancelEventArgs e)
{
    //if (lstPasswords.SelectedIndex == -1) e.Cancel = true;
    e.Cancel = (lstPasswords.SelectedIndex == -1);
}
like image 103
Fabian Avatar answered Sep 29 '22 04:09

Fabian


Easy,

    private void genPassMenu_Opening(object sender, CancelEventArgs e) 
    { 
        e.Cancel = (lstPasswords.SelectedIndex == 0); 

    } 
like image 22
Calanus Avatar answered Sep 29 '22 03:09

Calanus