0

GaugeControl的收藏中有三个仪表。我已经编写了它的双击事件处理程序,如下所示:

AddHandler gc.DoubleClick, AddressOf HandleGaugeDoubleClick

Private Sub HandleGaugeDoubleClick(sender As Object, e As EventArgs)
   'Gauge Information
End Sub

其中 gc 被标记为 GaugeControl 类型,并且其中添加了三个 GaugeControl。 在此处输入图像描述

我的问题是,我如何才能获得双击哪个仪表的信息?

请注意,这些仪表位于一个GaugeControl中,并在其集合中一一添加。我将如何获得被双击的仪表的信息。

编辑

第一次,这个代码片段运行良好,但NullReferenceException第二次点击同一个仪表时会给出第二次。

Dim hi As BasePrimitiveHitInfo = DirectCast(gc, IGaugeContainer).CalcHitInfo(e.Location) ' hi becomes Nothing when double clicked second time on same Gauge
If Not (TypeOf hi.Element Is DevExpress.XtraGauges.Core.Model.BaseGaugeModel) Then
    Dim model = DevExpress.XtraGauges.Core.Model.BaseGaugeModel.Find(hi.Element)
    If model IsNot Nothing AndAlso model.Owner IsNot Nothing Then
        gauge = model.Owner
    End If
End If

hi当双击同一个仪表时,这里的变量第二次变为空/无。当hi变为Nothing所以条件变为 false 并且剩余的代码产生NullReferenceException.

请参阅此代码段:

If (Not (gauge.Scales Is Nothing) And (gauge.Scales.Count > 0)) Then ' Actual exception here
    For i As Integer = 0 To gauge.Scales.Count - 1
        scaleComponent = gauge.Scales(i)
        cGaugeToBeShown.Scales.Add(scaleComponent)
    Next
End If

cGaugeToBeShown在哪里Dim cGaugeToBeShown As New CircularGauge

4

1 回答 1

0

我建议您使用GaugeControl.MouseDoubleClick事件,如下所示:

using DevExpress.XtraGauges.Base;
using DevExpress.XtraGauges.Core.Primitive;
//...
void gaugeControl1_MouseDoubleClick(object sender, MouseEventArgs e) {
    BasePrimitiveHitInfo hi = ((IGaugeContainer)gaugeControl1).CalcHitInfo(e.Location);
    if(!(hi.Element is DevExpress.XtraGauges.Core.Model.BaseGaugeModel)) {
        var model = DevExpress.XtraGauges.Core.Model.BaseGaugeModel.Find(hi.Element);
        if(model != null && model.Owner != null) {
            IGauge gauge = model.Owner;
            // do something with gauge
        }
    }
}
Imports DevExpress.XtraGauges.Base
Imports DevExpress.XtraGauges.Core.Primitive
'...
Private Sub gaugeControl1_MouseDoubleClick(sender As Object, e As MouseEventArgs)
    Dim hi As BasePrimitiveHitInfo = DirectCast(gaugeControl1, IGaugeContainer).CalcHitInfo(e.Location)
    If Not (TypeOf hi.Element Is DevExpress.XtraGauges.Core.Model.BaseGaugeModel) Then
        Dim model = DevExpress.XtraGauges.Core.Model.BaseGaugeModel.Find(hi.Element)
        If model IsNot Nothing AndAlso model.Owner IsNot Nothing Then
            Dim gauge As IGauge = model.Owner
            ' do something with gauge
        End If
    End If
End Sub

相关示例:如何提供与 GaugeControl 的自定义鼠标交互。

于 2014-09-03T14:44:36.037 回答