The context menu in the ZedGraphControl is built "on-the-fly" each time the user right clicks in the GraphPane area. Just after the context menu is constructed, but before actually displaying the menu, ZedGraphControl will pass control to any subscribers to the ContextMenuBuilder event. This gives you the opportunity to edit the menu before it gets displayed. A ContextMenuBuilder uses the ContextMenuBuilderEventHandler delegate, which looks like this:
public delegate void ContextMenuBuilderEventHandler( ZedGraphControl control, ContextMenuStrip menuStrip, Point mousePt,ZedGraphControl.ContextMenuObjectState objState );
- control
- A reference to the ZedGraphControl instance in which the menu will be displayed
- menuStrip
- A reference to the already-constructed ContextMenuStrip class instance
- mousePt
- A Point structure indicating the mouse location at the time the context menu was activated (typically, where the right-click action occurred)
To edit the context menu, first subscribe to the ContextMenuBuilder event. You can do this in the Forms designer in Visual Studio by right clicking on the ZedGraphControl in the form, select "properties", click the little yellow "lightning bolt" to get a list of events, click in the empty box to the right of ContextMenuBuilder, and hit enter. This will add ContextMenuEventHandler template to your code. If you are not using the designer, you can just add the following event to your code at the same point where you first construct the graph (typically in the Form_Load method):
zedGraphControl1.ContextMenuBuilder += new ZedGraphControl.ContextMenuBuilderEventHandler( MyContextMenuBuilder );
Note that the above assumes that the name of your ZedGraphControl is 'zedGraphControl1'.
Next, either edit the template provided by the Forms designer, or add a template if you are not using the designer as indicated below.
Removing or Disabling an Existing Menu Item
This code removes the menuitem "Set Scale to Default" from the context menu. If you just want to disable the item instead of removing it, comment out the "MenuItems.Remove" method call, and uncomment the "item.Enabled = false" statement.
private void MyContextMenuBuilder( ZedGraphControl control, ContextMenuStrip menuStrip, Point mousePt,ZedGraphControl.ContextMenuObjectState objState) { foreach( ToolStripMenuItem item in menuStrip.Items ) { if ( (string) item.Tag == "set_default" ) { // remove the menu item menuStrip.Items.Remove( item ); // or, just disable the item with this //item.Enabled = false; break; } } }
The item.Tag information was added after ZedGraph release version 4.3. This Tag information is actually the stringID information used internally to identify the localized strings to accomodate multiple languages. The following stringID's are used:
StringID | Menu Item |
---|---|
copy | Copy |
page_setup | Page Setup... |
Print... | |
save_as | Save Image As... |
set_default | Set Scale to Default |
show_val | Show Point Values |
undo_all | Undo All Zoom/Pan |
unzoom | Un-Zoom, Un-Pan, Undo Scroll |
:
Add a New Menu Item to the Context Menu
To add a brand new item to the context menu, your code would look something like this:
private void MyContextMenuBuilder( ZedGraphControl control, ContextMenuStrip menuStrip, Point mousePt,ZedGraphControl.ContextMenuObjectState objState ) { // create a new menu item ToolStripMenuItem item = new ToolStripMenuItem(); // This is the user-defined Tag so you can find this menu item later if necessary item.Name = "my_special_tag"; item.Tag = "my_special_tag"; // This is the text that will show up in the menu item.Text = "Do Something Special"; // Add a handler that will respond when that menu item is selected item.Click += new System.EventHandler( DoSomethingSpecial ); // Add the menu item to the menu menuStrip.Items.Add( item ); }
And the handler that responds when the menu item is selected would look something like this (this presumes that your ZedGraphControl is called zedGraphControl1):
protected void DoSomethingSpecial( object sender, System.EventArgs e ) { // do something here. For example, remove all curves from the graph zedGraphControl1.GraphPane.CurveList.Clear(); zedGraphControl1.Refresh(); }