您好我正在尝试向图表点添加点击事件,但是当我点击图表“对象引用未设置为对象的实例”时,我收到以下错误
这是我的代码
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
答案 0 :(得分:1)
如果鼠标光标位于控件上方,则控件将接收事件但不接收表单(对于解决方法,请参阅此问题:Winforms : Intercepting Mouse Event on Main Form first, not on Controls)。
因此Form1_MouseDown
无法触发,result
仍然Nothing
Chart1_Click
。
解决方法可能如下所示:
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