1

您好,我正在尝试向图表点添加单击事件,但是当我单击图表“对象引用未设置为对象的实例”时出现以下错误

这是我的代码

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
4

1 回答 1

1

如果鼠标光标位于控件上方,则只有控件将接收事件,但不会接收表单(有关解决方法,请参见例如此问题:Winforms : Intercepting Mouse Event on Main Form first, not on Controls)。

所以Form1_MouseDown不会开火,result仍然会NothingChart1_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
于 2012-07-16T14:35:55.920 回答