WinForm Dynamic Context Menu

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?anAybXNmdA==?=

    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_O pening(object sender, CancelEventArgs e) {
    if (sender.Equals( ListView1) == true) {
    mi_RawView.Visi ble = (0 < ListView1.Items .Count);
    } else {
    mi_FontArial.Vi sible = sender.Equals(T extBox1);
    mi_FontCourier. Visible = sender.Equals(T extBox1);
    mi_RawView.Visi ble = 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_O pening(object sender, CancelEventArgs e) {
    if (mi_EasterEggs. SourceControl == ListView1) {
    mi_RawView.Visi ble = (0 < ListView1.Items .Count);
    } else {
    mi_FontArial.Vi sible = (mi_EasterEggs. SourceControl == TextBox1);
    mi_FontCourier. Visible = (mi_EasterEggs. SourceControl == TextBox1);
    mi_RawView.Visi ble = 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?
  • =?Utf-8?B?Q2lhcmFuIE8nJ0Rvbm5lbGw=?=

    #2
    RE: WinForm Dynamic Context Menu

    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
    try{ Life(); } catch (TooDifficultException) { throw Toys(); }



    "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_O pening(object sender, CancelEventArgs e) {
    if (sender.Equals( ListView1) == true) {
    mi_RawView.Visi ble = (0 < ListView1.Items .Count);
    } else {
    mi_FontArial.Vi sible = sender.Equals(T extBox1);
    mi_FontCourier. Visible = sender.Equals(T extBox1);
    mi_RawView.Visi ble = 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_O pening(object sender, CancelEventArgs e) {
    if (mi_EasterEggs. SourceControl == ListView1) {
    mi_RawView.Visi ble = (0 < ListView1.Items .Count);
    } else {
    mi_FontArial.Vi sible = (mi_EasterEggs. SourceControl == TextBox1);
    mi_FontCourier. Visible = (mi_EasterEggs. SourceControl == TextBox1);
    mi_RawView.Visi ble = 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?

    Comment

    Working...