简体   繁体   English

vb.net图表控制点onclick事件

[英]vb.net chart control point onclick event

Hello i am trying to add a click event to chart points but i am getting the following Error when i click the chart "Object reference not set to an instance of an object" 您好,我试图将点击事件添加到图表点,但是单击图表“对象引用未设置为对象的实例”时出现以下错误

here is my code 这是我的代码

Private Sub Chart1_Click(sender As Object, e As System.EventArgs) Handles Chart1.Click
    Try
        Dim pointindex As Integer
        If result.ChartElementType = ChartElementType.DataPoint Then
            pointindex = result.PointIndex
        End If
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

Private Sub Form1_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
    result = Chart1.HitTest(e.X, e.Y)
End Sub

If the mouse cursor is above a control only the control will receive events but not the form (for workarounds see eg this question: Winforms : Intercepting Mouse Event on Main Form first, not on Controls ). 如果鼠标光标在控件上方,则仅该控件将接收事件,但不接收该窗体(有关变通方法,请参见此问题: Winforms:首先在主窗体上而不是在Controls上拦截鼠标事件 )。

So Form1_MouseDown will not fire and result will still be Nothing in Chart1_Click . 所以Form1_MouseDown不会着火和result仍然是NothingChart1_Click

A workaround could look like this: 解决方法如下所示:

Private Sub Chart1_Click(sender As Object, e As System.EventArgs) Handles Chart1.Click
    Try
        Dim pointindex As Integer
        Dim result As HitTestResult
        result = Chart1.HitTest(Cursor.Position.X, Cursor.Position.Y)
        If result.ChartElementType = ChartElementType.DataPoint Then
            pointindex = result.PointIndex
        End If
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM