Dynamic Context Menu

joe_pool_is

Contributor
Joined
Jan 18, 2004
Messages
507
Location
Longview, TX [USA]
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.

This context menu is included in 1 ListView control and 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:
Code:
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 some posts here on Context Menus and saw where Plausibly said to look at the .SourceControl field, so I modified my listing to this:
Code:
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 TextBox Control, but this is a bonus and is not necessary. First thing is first, right?
 
Ok, I've got this working, and it does great:
Code:
private void mi_EasterEggs_Opening(object sender, CancelEventArgs e) {
  if (sender.Equals(mi_EasterEggs) == true) {
    if (mi_EasterEggs.SourceControl is ListView) {
      mi_RawView.Visible = (0 < ListView1.Data.Count);
    } else if (mi_EasterEggs.SourceControl is TextBox) {
      bool visible = (((TextBox)mi_EasterEggs.SourceControl).Name == TextBox1.Name);
      mi_FontArial.Visible = visible;
      mi_FontCourier.Visible = visible;
      mi_RawView.Visible = false;
    }
  }
}
...except the Menu Items do *not* become visible. The 'visible' variable can be True, but as I step over the two Font Items, they remain False. If anyone knows what is up with this, please chime in!

I'm going to attempt accessing the Menu Items fromt the Context Menu instead of directly to see if this works.

Also, I still don't know the best way to impliment the default Context Menu...
 
When you step over the code - is it actually executing the lines
C#:
      mi_FontArial.Visible = visible;
      mi_FontCourier.Visible = visible;
      mi_RawView.Visible = false;
If so what is the value of the variable visible at this point?
 
Hi Plausibly.

The code works ...but it doesn't.

The value of 'bool visible' is set to False while both of the menu items' Visible Property is True.

I can hole my mouse over the Visible property of one of the menu items and it says 'False' while the 'bool visible' value says 'True.' I press F10 to step to the next line of execution and... Nothing! It just doesn't take.

All that being said, I found a way around it by accessing the individual members of the Context Menu by their item number instead of using the Menu Items directly:
Code:
bVisible = (((TextBox)mi_EasterEggs.SourceControl).Name == TextBox1.Name);
mi_EasterEggs.Items[0].Visible = bVisible;
mi_EasterEggs.Items[1].Visible = bVisible;
mi_EasterEggs.Items[2].Visible = false;
Why would this work when accessing the Menu Items by name does not?

Also (while I have your attention and now that I have the context menu working), is there a way that you know of to override the default Context Menu that comes with a TextBox so that I can just add to it? I'd like to keep the default 'copy, cut & paste' Context Menu and just add a few lines.
 
Back
Top