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
Try this:
private void genPassMenu_Opening(object sender, CancelEventArgs e)
{
//if (lstPasswords.SelectedIndex == -1) e.Cancel = true;
e.Cancel = (lstPasswords.SelectedIndex == -1);
}
Easy,
private void genPassMenu_Opening(object sender, CancelEventArgs e)
{
e.Cancel = (lstPasswords.SelectedIndex == 0);
}
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