473,581 Members | 2,757 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5897
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*******@digi talmetrology.co mwrote in message
news:u1******** *****@TK2MSFTNG P04.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**********@h otmail.comwrote in message
news:Og******** ******@TK2MSFTN GP04.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*******@digi talmetrology.co mwrote in message
news:u1******** *****@TK2MSFTNG P04.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
14171
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 "Select All" and "Clear Selection" to a checked list box? Thanks, Aaron Queenan.
5
5482
by: Dean L. Howen | last post by:
Dear friends, Could we determine when context menu should appear?
8
10081
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 a text box has 6 items, undo, cut, copy, paste, delete and select all. I would like to add one additional paste menu that opens a new sub menu with...
0
1184
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 editing or deleting the item from the list view. That works fine. However, I noticed that I can call the pop up context menu also by right clicking...
2
1997
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 in the system tray, the submenu item is there. After that, any subsequent right-clicks only show the same menu that was displayed at the first...
1
2709
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 the control I'm dynamically creating a context menu and assigning it to the ContextMenu property of the control -- think of this like the default...
1
6325
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 etc'). is there a way making this without writing my own context menu (meaning, popup form or something) ? Please don't post commercial products to...
2
23037
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 on both views). What i'm struggling to do when the menuItem_Click event is fired, is to detect the TreeNode or ListViewItem that was originally...
1
5581
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 loads. This context menu is included in one (1) ListView control and one (1) TextBox control.
0
7876
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7804
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8310
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7910
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6563
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5681
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5366
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3832
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2307
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.