Inherited Controls and Context Menus

P

Phill W.

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.ContextMenu and append to it?
Should use a separate function to do this "construction" 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 "ToolStripMenuItem"...)

TIA,
Phill W.
 
J

Jack Jackson

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.ContextMenu and append to it?
Should use a separate function to do this "construction" 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 "ToolStripMenuItem"...)

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 AddContextMenuItem. It
would have one parameter, an Enum with a value for each of the items
the base class adds, and it returns the appropriate ToolStripMenuItem.
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 ToolStripMenuItem.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Sharing of Context Menus 5
alter built-in ContextMenu 11
Inherited Treeview control with dynamic context menu. 1
context Menu 3
Context Menus 6
Popup Event not Firing 2
Context Menu Not Closing 4
Context Menus 1

Top