【问题标题】:GaugeControl double click event handlerGaugeControl 双击事件处理程序
【发布时间】:2014-09-03 11:56:40
【问题描述】:

我有GaugeControl 的集合中有三个仪表。我已经编写了它的双击事件处理程序如下:

AddHandler gc.DoubleClick, AddressOf HandleGaugeDoubleClick

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

其中 gc 被标记为 GaugeControl 类型,并且其中添加了三个 GaugeControl。

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

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

编辑

第一次,这段代码 sn-p 运行良好,但第二次点击同一个仪表时会给出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,因此条件变为假,剩余代码生成NullReferenceException

查看这段代码sn-p:

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

其中 cGaugeToBeShownDim cGaugeToBeShown As New CircularGauge

【问题讨论】:

    标签: .net vb.net winforms devexpress devexpress-windows-ui


    【解决方案1】:

    我建议你使用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
    

    相关示例:How to provide a custom mouse interaction with the GaugeControl.

    【讨论】:

    • 感谢您的帮助!当我粘贴上面的快照时,我如何才能获得第二个 CircularGaue?
    • @you 的问题听起来有点奇怪,因为所有仪表都可以通过 GaugeControl.Gauges 集合获得。我的解决方案提供对“点击”量表的访问。
    • 是的!我明白了。我对你的代码做了一些修改。将 Dim Gauge as IGauge 替换为 Dim Gauge As CircularGauge。虽然我还需要找到 Gauge 类型,但它现在可以工作了。谢谢你的回答。
    • 它工作正常,但是当我在同一个仪表上双击第二次时,它会给出 NullRefException。变量“hi”包含“Nothing”。你能帮忙吗?
    • @FaizanMubasher 无法用我的代码-sn-p 重现 NullRef ... 你修改过这个吗?如果是这样,请相应地更新您的问题 - 我稍后会检查它
    猜你喜欢
    • 2010-12-17
    • 2021-01-02
    • 2015-12-22
    • 2014-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-28
    相关资源
    最近更新 更多