Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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:

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:

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?

Posted

Ok, I've got this working, and it does great:

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...

Posted

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:

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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...