WinForm Dynamic Context Menu

J

jp2msft

I've got one Context Menu named mi_EasterEggs with three (3) menu items:

* mi_FontArial
* mi_FontCourier
* mi_RawData

All menu items have their Visible properties set to False when the form loads.

This context menu is included in one (1) ListView control and one (1)
TextBox control.

When one of these controls is right-clicked (to bring up the Context Menu),
I want to make menu items viisble depending on what item was right-clicked.

I tried this code, but the sender is always the mi_EasterEggs Menu Item:

private void mi_EasterEggs_Opening(object sender, CancelEventArgs e) {
if (sender.Equals(ListView1) == true) {
mi_RawView.Visible = (0 < ListView1.Items.Count);
} else {
mi_FontArial.Visible = sender.Equals(TextBox1);
mi_FontCourier.Visible = sender.Equals(TextBox1);
mi_RawView.Visible = false;
}
}

I searched google and found a suggestion by Plausibly Damp where he
mentioned usingt the .SourceControl field, so I modified my listing to this:

private void mi_EasterEggs_Opening(object sender, CancelEventArgs e) {
if (mi_EasterEggs.SourceControl == ListView1) {
mi_RawView.Visible = (0 < ListView1.Items.Count);
} else {
mi_FontArial.Visible = (mi_EasterEggs.SourceControl == TextBox1);
mi_FontCourier.Visible = (mi_EasterEggs.SourceControl == TextBox1);
mi_RawView.Visible = false;
}
}

It still doesn't work, though.

What am I doing wrong?

Also, I'd like to still have the default Context Menu for the Controls, but
this is a bonus and is not necessary.

First thing is first, right?
 
C

Ciaran O''Donnell

From memory, I think the contect menu wont open unless it has something to
display, change the logic so at form load the items are visible, then hide
the non-required ones when opening the menu rather than the other way round.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top