473,399 Members | 2,159 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Adding menu items to a control's context menu

MCM
I'm working on a plotting control. The plotting control will have a context
menu with basic commands for "scaling", "zooming", etc. Is there a way
that, from the parent form, I can add more commands to the control's context
menu?

I'm envisioning a case where the control has a set of context menu items,
and the parent form also has a set of context menu items.

Thanks.
Mar 7 '07 #1
2 5884
The ContextMenu class has a Popup event. In other words, just before the
ContextMenu is about to appear, the Popup event fires. The good thing about
this event is that it permits you to construct your ContextMenu *items*
on-the-fly. This means you can construct them or change their attributes
(ticked, disabled) etc. This on-demand behaviour is *much* better than
trying to keep a static menu "up to date", especially if items are being
ticked or disabled. For example, we might disable a "Delete" option if the
user has no permission to delete a row of data in a list.

By using the event to trigger the construction of the menu, you can receive
notification when the menu is required, and then construct it appropriately
based on currently known state. In your case that means combining your
control's context menu items with those of its parent, or of the form.

The object that receives the event needs to be in a position to grab the
menu items from both the control, and the form (and anything else), in order
to build a menu. We get around this by having a sub-classed "form" and a
sub-classed "control" that each possess the correct interfaces for talking
to one another.

A simpler method might be to have two handlers for the Popup event - one
pointing to the control and one pointing to the form. Each then contributes
their own items to the context menu. The only problem is that neither event
handler is "in control" - which makes it harder to code in my opinion.

In our particular implementation, we use the interface technique because we
want each component (form, control) to contribute its menu items, but we
want the ability for them to "blend together" if necessary. For example,
menu item #1 might be the form's, #2 is the control's, #3 is the form's etc.
Each component contributing menu items defines a "rank" for each of their
items, and the combination of all ranks determines the final sequence.
Separators can also be injected into the menu, again based on rank. Plus we
have additional logic to ensure that a separator is never at the beginning
or end of the context menu, there are never two separators adjacent to one
another, etc. By having one thing "in control" of the construction of the
context menu, we have reasonable decoupling without either component
worrying how they might "affect" the other (practically speaking, ranks
still need to be chosen pragmatically to ensure the menu sequence comes out
as intended).

Hope this helps.

Kevin

"MCM" <mc*******@digitalmetrology.comwrote in message
news:u1*************@TK2MSFTNGP04.phx.gbl...
I'm working on a plotting control. The plotting control will have a
context menu with basic commands for "scaling", "zooming", etc. Is there
a way that, from the parent form, I can add more commands to the control's
context menu?

I'm envisioning a case where the control has a set of context menu items,
and the parent form also has a set of context menu items.

Thanks.


Mar 7 '07 #2
MCM
Wow. Thanks Kevin,

You are doing exactly what I want to be doing. :) However, I'm going to
have to sort through this a bit more - you a slightly over my head in terms
of my C# abilities. Nonetheless, you've given me some very good advice,
I'll just have to do some Googling to come up with the code to support your
recommendation.

Thanks,

- Mark
"Kevin Frey" <ke**********@hotmail.comwrote in message
news:Og**************@TK2MSFTNGP04.phx.gbl...
The ContextMenu class has a Popup event. In other words, just before the
ContextMenu is about to appear, the Popup event fires. The good thing
about this event is that it permits you to construct your ContextMenu
*items* on-the-fly. This means you can construct them or change their
attributes (ticked, disabled) etc. This on-demand behaviour is *much*
better than trying to keep a static menu "up to date", especially if items
are being ticked or disabled. For example, we might disable a "Delete"
option if the user has no permission to delete a row of data in a list.

By using the event to trigger the construction of the menu, you can
receive notification when the menu is required, and then construct it
appropriately based on currently known state. In your case that means
combining your control's context menu items with those of its parent, or
of the form.

The object that receives the event needs to be in a position to grab the
menu items from both the control, and the form (and anything else), in
order to build a menu. We get around this by having a sub-classed "form"
and a sub-classed "control" that each possess the correct interfaces for
talking to one another.

A simpler method might be to have two handlers for the Popup event - one
pointing to the control and one pointing to the form. Each then
contributes their own items to the context menu. The only problem is that
neither event handler is "in control" - which makes it harder to code in
my opinion.

In our particular implementation, we use the interface technique because
we want each component (form, control) to contribute its menu items, but
we want the ability for them to "blend together" if necessary. For
example, menu item #1 might be the form's, #2 is the control's, #3 is the
form's etc. Each component contributing menu items defines a "rank" for
each of their items, and the combination of all ranks determines the final
sequence. Separators can also be injected into the menu, again based on
rank. Plus we have additional logic to ensure that a separator is never at
the beginning or end of the context menu, there are never two separators
adjacent to one another, etc. By having one thing "in control" of the
construction of the context menu, we have reasonable decoupling without
either component worrying how they might "affect" the other (practically
speaking, ranks still need to be chosen pragmatically to ensure the menu
sequence comes out as intended).

Hope this helps.

Kevin

"MCM" <mc*******@digitalmetrology.comwrote in message
news:u1*************@TK2MSFTNGP04.phx.gbl...
>I'm working on a plotting control. The plotting control will have a
context menu with basic commands for "scaling", "zooming", etc. Is there
a way that, from the parent form, I can add more commands to the
control's context menu?

I'm envisioning a case where the control has a set of context menu items,
and the parent form also has a set of context menu items.

Thanks.



Mar 8 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Aaron Queenan | last post by:
How can I use the designer to add a context menu to a class which inherits from a control, e.g. treeview, without adding the context menu to a form? For example, to add a context menu with...
5
by: Dean L. Howen | last post by:
Dear friends, Could we determine when context menu should appear?
8
by: Dennis C. Drumm | last post by:
Is there a way to modify the standard context menu shown when someone right clicks in a windows text box and that would work for all open windows applications? The standard context menu for...
0
by: Dino M. Buljubasic | last post by:
I have several context menu added to my forms. The forms are displaying items in a list view. When I click on an item in a list view, a popup context menu shows allowing me to chese between...
2
by: PJSimon | last post by:
I have a system tray icon for my application. Each time the user pastes some text into the application, I add a submenu item in the system tray's context menu. The first time I right-click the icon...
1
by: Chris Murphy via DotNetMonster.com | last post by:
Hi all, I'm just wondering if any one can help me with this development issue I'm having. I've created a customized treeview control to handle the particular tasks to which I'll be using it. Within...
1
by: goRide | last post by:
Hi, I'm looking of a way (preferred - a ready class or dll) to customize the context menu. many application has more controls inside the context menu (like textbox, sliders, checkbox, panel...
2
by: Bry | last post by:
I have a context menu which is shared between a TreeView and ListView control (both controls show the same information, similar to how Windows Explorer works, so it makes sense to use the same menu...
1
by: =?Utf-8?B?anAybXNmdA==?= | last post by:
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.