我正在使用Windows窗体在C#中创建GUI。要以图形表示形式查看数据,我正在使用.Net v3的许可TeeChart。我想在TeeChart中实现鼠标单击事件。我有VB6代码,因为GUI是以前在VB6中创建的。我已经将VB6代码转换为C#,但仍然有一些问题。我想在TeeChart上创建鼠标单击弹出窗口。下面的代码显示Vb6代码以在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
我已经将上面的代码转换为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轴位置的鼠标单击上创建弹出菜单。所以请帮我
提前致谢。
最佳答案
感谢您的澄清。我已经对您的代码进行了修改,使其成为一个简单的示例,其中我将ContextMenu与在MouseUp事件中计算的Xscreenvalues一起使用,并且我认为您可以作为下一个代码来做些事情:
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));
}
}
您能否告诉我们以前的代码是否可以奏效?
希望对您有所帮助。
谢谢,