Inherited Controls and Context Menus

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Phill W.

    Inherited Controls and Context Menus

    What's the "proper" way of handling this?

    I have a base Control that offers a small set of options via its
    ContextMenu.

    I've now inherited from this and would like to add additional items to
    this Context Menu structure but how best to do so?

    Do I override the ContextMenu property in the sub-class, grab the value
    of MyBase.ContextM enu and append to it?
    Should use a separate function to do this "constructi on" and assign the
    result to the ContextMenu property from Sub New?
    (other options, please)

    And then ...

    What if I need to modify the existing items (defined in the base
    Control)? Is there a "clean" way to support this, or do I just have to
    search the whole menu structure to find a given item by name?

    (Of course, by "MenuItem" I mean "ToolStripMenuI tem"...)

    TIA,
    Phill W.
  • Jack Jackson

    #2
    Re: Inherited Controls and Context Menus

    On Thu, 09 Oct 2008 12:55:08 +0100, "Phill W."
    <p-.-a-.-w-a-r-d-@-o-p-e-n-.-a-c-.-u-kwrote:
    >What's the "proper" way of handling this?
    >
    >I have a base Control that offers a small set of options via its
    >ContextMenu.
    >
    >I've now inherited from this and would like to add additional items to
    >this Context Menu structure but how best to do so?
    >
    >Do I override the ContextMenu property in the sub-class, grab the value
    >of MyBase.ContextM enu and append to it?
    >Should use a separate function to do this "constructi on" and assign the
    >result to the ContextMenu property from Sub New?
    >(other options, please)
    >
    >And then ...
    >
    >What if I need to modify the existing items (defined in the base
    >Control)? Is there a "clean" way to support this, or do I just have to
    >search the whole menu structure to find a given item by name?
    >
    >(Of course, by "MenuItem" I mean "ToolStripMenuI tem"...)
    >
    >TIA,
    Phill W.
    There are various ways to do this. What I might do is to create an
    Overridable function in the base class called AddContextMenuI tem. It
    would have one parameter, an Enum with a value for each of the items
    the base class adds, and it returns the appropriate ToolStripMenuIt em.
    In the base class call this method for each item you want to add. If
    it doesn't return Nothing, add the return value to the context menu.

    In the derived class override this method. If you want to suppress an
    item, return Nothing without calling the base class implementation. If
    you want to modify an item, call the base class implementation and
    then modify the returned ToolStripMenuIt em.

    You might also want to have an overridable sub in the base class that
    is just an empty method with two parameters, the context menu and a
    flag, and call it in the base class before adding any items and again
    after all items are added. The derived class could override this to
    get control both before any items are added and again after.

    Comment

    Working...