Search code examples
.netvb.netwinformsdevexpressdevexpress-windows-ui

GaugeControl double click event handler


I have GaugeControl having three gauges in its collection. I have written its double click event handler as follows:

AddHandler gc.DoubleClick, AddressOf HandleGaugeDoubleClick

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

where gc is decalred to be of type GaugeControl and three GaugeControls have been added in it. enter image description here

My question is, how I can get the information of which Gauge has been double clicked?

Note that these gauges are in one GaugeControl and added one by one in its collection. How will I be able to obtain the info of the Gauges that was double clicked.

EDIT

First time, this code snippet runs fine, but gives NullReferenceException second time when clicked on same Gauge.

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

Here variable hi becomes null/Nothing second time when double clicked on same Gauge. As hi becomes Nothing so condition becomes false and remaining code produces NullReferenceException.

See this code snippet:

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

Where cGaugeToBeShown is Dim cGaugeToBeShown As New CircularGauge.


Solution

  • I suggest you use the GaugeControl.MouseDoubleClick event as follows:

    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
    

    Related example: How to provide a custom mouse interaction with the GaugeControl.