2014-09-03 72 views
0

我有GaugeControl其集合中有三個量表。GaugeControl雙擊事件處理程序

AddHandler gc.DoubleClick, AddressOf HandleGaugeDoubleClick 

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

其中GC是decalred爲類型GaugeControl和三個GaugeControls都在裏面添加了:我有如下的書面其雙擊事件處理程序。 enter image description here

我的問題是,我怎麼能得到哪個Gauge已被雙擊的信息?

注意,這些儀表是一個GaugeControl,並通過它的一個集合中添加一個。我將如何能夠獲得雙擊的Gauges信息。

編輯

第一次,這個代碼片斷運行良好,但在相同規格點擊時給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變成沒有所以條件變爲假而剩餘的代碼產生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 

cGaugeToBeShownDim cGaugeToBeShown As New CircularGauge

回答

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 

相關例子:How to provide a custom mouse interaction with the GaugeControl.

+0

感謝您的幫助!當我貼上快照時,我如何獲得第二個CircularGaue? – 2014-09-04 05:01:36

+0

@你的問題聽起來有點奇怪,因爲所有的儀表都可以通過GaugeControl.Gauges集合獲得。我的解決方案提供對「點擊」表的訪問。 – DmitryG 2014-09-04 06:34:34

+0

是啊!我明白了。我在代碼中做了一些修改。用Dim Gauge替換Dim Gauge as IGauge作爲CircularGauge。雖然我也需要找到量表類型,但現在它的工作。感謝您的回答。 – 2014-09-04 06:43:16