468,548 Members | 1,886 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,548 developers. It's quick & easy.

WinForm Dynamic Context Menu

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?
Oct 8 '08 #1
1 5267
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.
--
Ciaran O''Donnell
http://wannabedeveloper.spaces.live.com
"jp2msft" wrote:
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?
Oct 9 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Eddie de Bear | last post: by
8 posts views Thread by Dennis C. Drumm | last post: by
6 posts views Thread by QuenHo | last post: by
1 post views Thread by Chris Murphy via DotNetMonster.com | last post: by
3 posts views Thread by RahimAsif | last post: by
2 posts views Thread by Ron M. Newman | last post: by
1 post views Thread by UniDue | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.