2012-06-28 56 views
1

我正在Asp.net中寫一個儀表板應用程序。當我加載主頁時,我捕獲控制設置並動態創建量表,圖表,圖像等,並將它們加載到HTML表格中的單元格中。提供給每個控件的數據將每3秒更新一次。我目前將我的儀表存儲在一個名爲BoldGauge的類中。該類內部有一個GaugeValue屬性,需要每3秒更新一次。如何循環遍歷類並在運行時爲每個創建的量表更改值?我的階級是這樣的:循環遍歷C#中的類並更改每個對象的屬性值

public class BoldGauge 
{ 
    private ASPxGaugeControl m_Gauge; 
    private int m_GaugeTimer; 
    private string m_GaugeValue; 
    private string m_GaugeDataType; 
    private float m_GaugeMinValue; 
    private float m_GaugeMaxValue; 

    public BoldGauge(ASPxGaugeControl Gauge, string GaugeValue, string GaugeDataType, float GaugeMinValue, float GaugeMaxValue) 
    { 
     m_Gauge = Gauge; 
     m_GaugeValue = GaugeValue; 
     m_GaugeDataType = GaugeDataType; 
     m_GaugeMinValue = GaugeMinValue; 
     m_GaugeMaxValue = GaugeMaxValue; 

    } 

    public string GaugeValue 
    { 
     get 
     { 
      return m_GaugeValue; 
     } 
     set 
     { 
      m_GaugeValue = value; 
     } 
    } 

    public int GaugeTimer 
    { 
     get 
     { 
      return m_GaugeTimer; 
     } 
     set 
     { 
      m_GaugeTimer = value; 
     } 
    } 

    public string GaugeDataType 
    { 
     get 
     { 
      return m_GaugeDataType; 
     } 
     set 
     { 
      m_GaugeDataType = value; 
     } 
    } 

    public ASPxGaugeControl Gauge 
    { 
     get 
     { 
      return m_Gauge; 
     } 
     set 
     { 
      m_Gauge = value; 
     } 
    } 

    public float GaugeMinValue 
    { 
     get 
     { 
      return m_GaugeMinValue; 
     } 
     set 
     { 
      m_GaugeMinValue = value; 
     } 
    } 

    public float GaugeMaxValue 
    { 
     get 
     { 
      return m_GaugeMaxValue; 
     } 
     set 
     { 
      m_GaugeMaxValue = value; 
     } 
    } 
} 

在主頁上我生成新的對象就像這樣:

newBoldGauge = new BoldGauge(boldGaugeControl, gaugeValue, controlData, minimumGaugeValue, MaximumGaugeValue); 

感謝。

+0

你是如何在頁面上更新的控制計劃? –

回答

1

創建集合的BoldGague對象,然後遍歷該集合並進行必要的更改。

List<BoldGague> gagues = new List<BoldGague>(); 

foreach(BoldGague gague in gagues) 
{ 
    // change values here. 
} 
+0

感謝大家的幫助!我可以接受所有這些答案,因爲它們都有效嗎? – CodeMan5000

1

您需要一個集合來存儲您的所有量表。如果有這樣的情況,您可以簡單地遍歷集合並依次更新每個量表。

var gauges = new List<BoldGague>() 

gauges.Add(new BoldGauge(boldGaugeControl, gaugeValue, controlData, 
    minimumGaugeValue, MaximumGaugeValue)); 
... 

foreach(var gauge in gauges) 
{ 
    // update the value 
} 

或者,你可以把它們放在一個字典,然後按名稱進行更新:

var gauges = new Dictionary<string, BoldGauge>(); 

gauges.add("name of gauge", new BoldGauge(boldGaugeControl, gaugeValue, 
    controlData, minimumGaugeValue, MaximumGaugeValue)); 

... 

// Update the value of a named gauge 
gauges["name of gauge"].GaugeValue = newValue; 
1
IEnumerable<Control> EnumerateControlsRecursive(Control parent) 
{ 
    foreach (Control child in parent.Controls) 
    { 
     yield return child; 
     foreach (Control descendant in EnumerateControlsRecursive(child)) 
      yield return descendant; 
    } 
} 

,並使用它像這樣:

foreach (Control c in EnumerateControlsRecursive(Page)) 
    { 
     if(c is BoldGauge) 
     { 
      BoldGauge.GaugeValue = "some value" 
     } 
    } 
1

只是一個供參考,使用更新自動getter/setter方法,你可以重寫你喜歡這個確切類:

public class BoldGauge 
{ 
    public BoldGauge(ASPxGaugeControl gauge, string gaugeValue, string gaugeDataType, float gaugeMinValue, float gaugeMaxValue) 
    { 
     Gauge = gauge; 
     GaugeValue = gaugeValue; 
     GaugeDataType = gaugeDataType; 
     GaugeMinValue = gaugeMinValue; 
     GaugeMaxValue = gaugeMaxValue;  
    } 

    public string GaugeValue { get; set; } 
    public int GaugeTimer { get; set; } 
    public string GaugeDataType { get; set; } 
    public ASPxGaugeControl Gauge { get; set; } 
    public float GaugeMinValue { get; set; } 
    public float GaugeMaxValue { get; set; } 
} 
+1

謝謝,這看起來更清潔! – CodeMan5000