2

我正在使用 Windows 窗体在 C# 中创建 GUI。为了以图形表示形式查看数据,我使用了 .Net v3 的许可 TeeChart。我想在 TeeChart 中实现鼠标点击事件。我有 VB6 代码,因为 GUI 之前是在 VB6 中创建的。我已将该 VB6 代码转换为 C#,但仍有一些问题。我想在 TeeChart 上创建鼠标点击弹出窗口。下面的代码显示了在 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 轴位置创建弹出菜单。所以请帮我解决这个问题。

提前致谢。

4

1 回答 1

1

感谢您的澄清。我已经修改了您的代码以制作一个简单的示例,其中我使用了带有 Xscreenvalues 的 ContextMenu,这些值在 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));
      }
  }

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

我希望会有所帮助。

谢谢,

于 2013-02-19T12:24:52.683 回答