如何在TeeChart for.net中添加鼠标单击事件

本文关键字:添加 鼠标 单击 事件 net TeeChart for | 更新日期: 2023-09-27 18:00:09

我正在使用Windows窗体在C#中创建GUI。为了以图形表示方式查看数据,我正在使用.Net v3的许可TeeChart。我想在TeeChart中实现鼠标点击事件。我有VB6代码,因为GUI是以前在VB6中创建的。我已经把那个VB6代码转换成了C#,但还是有一些问题。我想在TeeChart上创建鼠标点击弹出。下面的代码显示了在发球台上创建鼠标点击弹出窗口的Vb6代码。

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

我已将上述代码转换为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"));
}

但我无法使用上面的代码在TeeChart上创建弹出窗口。我想创建弹出菜单鼠标点击x轴的位置。所以请帮帮我。

提前谢谢。

如何在TeeChart for.net中添加鼠标单击事件

感谢您的澄清。我已经调整了你的代码,以制作一个简单的例子,其中我使用了一个ContextMenu,其中Xscreenvalue是在MouseUp事件中计算的,我认为你可以做一些事情作为下一个代码:

    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));
      }
  }

你能告诉我们以前的代码是否适用于你吗?

我希望这会有所帮助。

谢谢,