Nevron Forum

Right-click context menu customization

https://www.nevron.com/Forum/Topic5708.aspx

By Santosh Chauhan - Thursday, October 13, 2011

Hi,

 

I want to customize the right-click context menu for my Nevron chart control to get options like Chart Editor, Projections, Tools etc in the context menu. Could you please help me get that done. A code sample would be of great help.

 

I tried using the context menu present under toolbars of Nevron.Chart.WinForm.NChartCommandBarsManager; but it doesn’t work.

 

kind regards

Santosh

By Nevron Support - Thursday, October 13, 2011

Hi Santosh,

You can easily display a context menu in response to mouse click event - the following code snippet shows how to show a dynamic context menu that contains two elements in case you click on a bar or the rest of the control:

  private void Form1_Load(object sender, EventArgs e)
  {
   NChart chart = nChartControl1.Charts[0];

   NBarSeries bar = new NBarSeries();

   bar.Values.Add(10);
   bar.Values.Add(20);
   bar.Values.Add(30);

   chart.Series.Add(bar);

   nChartControl1.MouseClick += new MouseEventHandler(nChartControl1_MouseClick);
  }

  void nChartControl1_MouseClick(object sender, MouseEventArgs e)
  {
   NHitTestResult hitTest = nChartControl1.HitTest(e.X, e.Y);
   ContextMenu menu = new ContextMenu();

   if (hitTest.ChartElement == ChartElement.DataPoint)
   {
    MenuItem barMenuItem = new MenuItem();
    barMenuItem.Click +=new EventHandler(barMenuItem_Click);
    barMenuItem.Tag = hitTest.DataPointIndex;
    barMenuItem.Text = "Toggle Fill";
    menu.MenuItems.Add(barMenuItem);
   }

   MenuItem editorMenuItem = new MenuItem();
   editorMenuItem.Click +=new EventHandler(editorMenuItem_Click);
   editorMenuItem.Text = "Show Editor...";
   menu.MenuItems.Add(editorMenuItem);

   menu.Show(nChartControl1, new Point(e.X, e.Y));
  }

  void editorMenuItem_Click(object sender, EventArgs e)
  {
   nChartControl1.ShowEditor();
  }

  void barMenuItem_Click(object sender, EventArgs e)
  {
   NChart chart = nChartControl1.Charts[0];
   NBarSeries bar = (NBarSeries)chart.Series[0];

   int barIndex = (int)((MenuItem)sender).Tag;

   if (bar.FillStyles[barIndex] == null)
   {
    bar.FillStyles[barIndex] = new NColorFillStyle(Color.Red);
   }
   else
   {
    bar.FillStyles[barIndex] = null;
   }

   nChartControl1.Refresh();
  }

Hope this helps - let us know if you meet any problems.