1

我正在用 Asp.net 编写仪表板应用程序。当我加载主页时,我会捕获控制设置并动态创建仪表、图表、图像等,并将它们加载到 HTML 表格中的单元格中。将提供给每个控件的数据将每 3 秒更新一次。我目前将仪表存储在一个名为 BoldGauge 的类中。在该类内部有一个 GaugeValue 属性,需要每 3 秒更新一次。如何循环遍历类并在运行时更改创建的每个仪表的值?我的课看起来像这样:

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);

谢谢。

4

4 回答 4

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;
于 2012-06-28T20:48:01.197 回答
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"
        }
    }
于 2012-06-28T20:49:00.613 回答
1

创建一个BoldGague对象集合,然后遍历该集合并进行必要的更改。

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

foreach(BoldGague gague in gagues)
{
    // change values here.
}
于 2012-06-28T20:49:44.570 回答
1

仅供参考,使用较新的自动获取器/设置器,您可以像这样重写您的确切类:

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; }
}
于 2012-06-28T21:00:03.130 回答