0

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.

Faizan Mubasher
  • 4,427
  • 11
  • 45
  • 81

1 Answers1

0

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.

DmitryG
  • 17,677
  • 1
  • 30
  • 53
  • Thanks for your help! As I pasted above snapshot, how I can get second CircularGaue? – Faizan Mubasher Sep 04 '14 at 05:01
  • @you question sounds a bit strange because all the gauges are available via the GaugeControl.Gauges collection. My solution provides access to the "clicked" gauge. – DmitryG Sep 04 '14 at 06:34
  • Yeah! I got it.I have done a bit amendment in your code. Replaced Dim Gauge as IGauge with Dim gauge As CircularGauge. Though I also need to find the Gauge type but its working now. Thanks for your answer. – Faizan Mubasher Sep 04 '14 at 06:43
  • it works fine but when I double click second time on same gauge it gives NullRefException. Variable "hi" contains "Nothing". Can you help? – Faizan Mubasher Sep 04 '14 at 07:31
  • @FaizanMubasher can't reproduce the NullRef with my code-snippet... Have you modified this one? If so, please update you question accordingly - i will check it a bit later – DmitryG Sep 04 '14 at 07:34