Search code examples
c#winformsmouseeventteechart

how to add mouse click events in TeeChart for .net


I am creating GUI in C# using Windows Form. To view data in graphical representation I am using licensed TeeChart for .Net v3. I want to implement mouse click events in TeeChart. I have the VB6 code for that because the GUI is created in VB6 previously. I have converted that VB6 code into C# but still i have some problems. I want to create mouse click pop on TeeChart. The code below shows Vb6 code to create mouse click popup on teechart.

Private Sub TChart1_OnMouseUp(ByVal Button As TeeChart.EMouseButton, ByVal Shift As TeeChart.EShiftState, ByVal x As Long, ByVal y As Long)

If Not m_objTransfer Is Nothing Then

    If chkGraphVolume.value = vbChecked And Button = mbRight Then
        'MsgBox TChart1.Series(0).XValueToText(x)
        'MsgBox TChart1.Series(0).XValueToText(TChart1.Series(0).XScreenToValue(x))

        'MsgBox TChart1.Series(0).XScreenToValue(x)

        m_dblTempVolFromTo = Round(TChart1.Series(0).XValueToText(TChart1.Series(0).XScreenToValue(x)))

        mnuPopupChartFrom.Caption = "From " & m_dblTempVolFromTo & " cc"
        mnuPopupChartTo.Caption = "To " & m_dblTempVolFromTo & " cc"


        PopupMenu mnuPopupChart

    ElseIf chkGraphVolume.value = vbUnchecked And Button = mbRight Then
        Debug.Print CDate(TChart1.Series(0).XScreenToValue(x))
        mnuPopupChartFrom.Caption = "From " & CDate(TChart1.Series(0).XScreenToValue(x))
        mnuPopupChartTo.Caption = "To " & CDate(TChart1.Series(0).XScreenToValue(x))
        m_dtTempTimeFromTo = CDate(TChart1.Series(0).XScreenToValue(x))

        PopupMenu mnuPopupChart

    End If

End If
Debug.Print "From " TChart1.Series(0).XValueToText(TChart1.Series(0).XScreenToValue(x)) & " cc   
 End Sub

I have converted the above code into C#

private void TChart1_OnMouseUp(TeeChart.EMouseButton Button, TeeChart.EShiftState Shift, long x, long y) {
    if (!(m_objTransfer == null)) {
        if (((chkGraphVolume.value == vbChecked) 
                    && (Button == mbRight))) {
            // MsgBox TChart1.Series(0).XValueToText(x)
            // MsgBox TChart1.Series(0).XValueToText(TChart1.Series(0).XScreenToValue(x))
            // MsgBox TChart1.Series(0).XScreenToValue(x)
            m_dblTempVolFromTo = Round(TChart1.Series(0).XValueToText(TChart1.Series(0).XScreenToValue(x)));
            mnuPopupChartFrom.Caption = ("From " 
                        + (m_dblTempVolFromTo + " cc"));
            mnuPopupChartTo.Caption = ("To " 
                        + (m_dblTempVolFromTo + " cc"));
            PopupMenu;
            mnuPopupChart;
        }
        else if (((chkGraphVolume.value == vbUnchecked) 
                    && (Button == mbRight))) {
            Debug.Print;
            DateTime.Parse(TChart1.Series(0).XScreenToValue(x));
            mnuPopupChartFrom.Caption = ("From " + DateTime.Parse(TChart1.Series(0).XScreenToValue(x)));
            mnuPopupChartTo.Caption = ("To " + DateTime.Parse(TChart1.Series(0).XScreenToValue(x)));
            m_dtTempTimeFromTo = DateTime.Parse(TChart1.Series(0).XScreenToValue(x));
            PopupMenu;
            mnuPopupChart;
        }
    }
    Debug.Print;
    ("From " + (TChart1.Series(0).XValueToText(TChart1.Series(0).XScreenToValue(x)) + " cc"));
}

But i am not able to use the above code to create popup on TeeChart. I want to create pop up menu on mouse click with x-axis location. so please help me with this.

Thanks in advance.


Solution

  • Thanks for your clarification. I have adapted your code to make a simple example where I have used a ContextMenu with Xscreenvalues that are calculated in MouseUp event and I think you can do something as next code:

        ContextMenu ContextMenu1;
        MenuItem menuItem1;
        MenuItem menuItem2;
        public Form1()
        {
            InitializeComponent();
            ContextMenu1 = new System.Windows.Forms.ContextMenu();
            menuItem1 = new System.Windows.Forms.MenuItem();
            menuItem2 = new System.Windows.Forms.MenuItem();
            ContextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { menuItem1, menuItem2 });
            InitializeChart();
        }
      Steema.TeeChart.Styles.Line line; 
      private void InitializeChart()
      {
          line = new Line(tChart1.Chart);
          line.FillSampleValues();
          tChart1.MouseUp  = new MouseEventHandler(tChart1_MouseUp);
      }
    
    
      void tChart1_MouseUp(object sender, MouseEventArgs e)
      {
          if (e.Button == System.Windows.Forms.MouseButtons.Right)
          {
              menuItem1.Index = 0;
              menuItem1.Text = "From:"   Math.Round(tChart1[0].XScreenToValue(e.X)).ToString();
              menuItem2.Index = 1;
              menuItem2.Text = "To:"   Math.Round(tChart1[0].XScreenToValue(e.X)).ToString();
              ContextMenu1.Show(tChart1, new Point(e.X, e.Y));
          }
      }
    

    Could you please, tell us if previous code works in your end?

    I hope will helps.

    Thanks,