【问题标题】:Better for loop performance to check radiobuttons更好的循环性能来检查单选按钮
【发布时间】:2016-11-08 17:44:23
【问题描述】:

我有 512 个单独的 radio buttons,每个都与要显示的 bool 相关。

我用for loop 做了一个函数,但它使用了大量的CPU。有没有更好的方法来做到这一点?

for (int k = 0; k < NUMWORDS_FOR_PLCTOHMI_BOOLS * 16; k++)
{
    string sRadioButtonName = "radioButton_PLCtoHMI_Bool" + k.ToString();
    RadioButton r = Controls.Find(sRadioButtonName, true)[0] as RadioButton;
    r.Checked = KepwarearrWordtoBools_PLCtoHMI[k];
}

【问题讨论】:

  • 您可以使用 foreach 循环。这是winform还是WPF??
  • 我认为 Controls.find 使用了大量的 CPU。你怎么看?

标签: c# for-loop radio-button


【解决方案1】:

您可以先检索所有单选按钮,然后在内存中迭代它们,如下所示:

var radioButtons = this.Controls.OfType<RadioButton>();

for (int k = 0; k < NUMWORDS_FOR_PLCTOHMI_BOOLS * 16; k++)
{
    string sRadioButtonName = "radioButton_PLCtoHMI_Bool" + k.ToString();
    RadioButton r = radioButtons.Single(x => x.Name == sRadioButtonName);
    r.Checked = KepwarearrWordtoBools_PLCtoHMI[k];
}

这样应该更有效率。

【讨论】:

  • 这假定所有单选按钮都直接包含在 this 容器中。如果它们分散在不同的容器中,这将不起作用
【解决方案2】:

如果控件在列表中,如何做到这一点的示例:

List<RadioButton> radioButtons = new List<RadioButton>(); 
//Fill the List with the controls

for (int k = 0; k < radioButtons.Count; k++)
{
   radioButtons[k].Checked = KepwarearrWordtoBools_PLCtoHMI[k];
}

唯一剩下的就是填写List

【讨论】:

    【解决方案3】:

    我的建议是创建一个字典,因为它查找项目的访问时间非常快。

    当您创建按钮时:

    private Dictionary<String, RadioButton> buttons = new Dictionary<String, RadioButton>();
    
    //Wherever you create those buttons
    buttons.Add("radiobutton_PLCtoHMI_Bool" + k.toString());
    
    //When you want to get them
    for (int k = 0; k < NUMWORDS_FOR_PLCTOHMI_BOOLS * 16; k++)
    {
        string sRadioButtonName = "radioButton_PLCtoHMI_Bool" + k.ToString();
        //You find it faster than with Controls.Find()
        RadioButton r = buttons(sRadioButtonName);
        r.Checked = KepwarearrWordtoBools_PLCtoHMI[k];
    }
    

    【讨论】:

      【解决方案4】:
      foreach (var rb in this.Controls.OfType<RadioButton>())
      {
         var k = rb.Name.Substring(25); // because "radioButton_PLCtoHMI_Bool" has 25 characters
         int i;
         if(int.TryParse(k, out i)) //if k is an integer 
            rb.Checked = KepwarearrWordtoBools_PLCtoHMI[i];
      }
      

      【讨论】:

      • 您可以提供更多背景信息来说明您选择此代码的原因。 @tebdilikyafet 提供的代码有什么好处
      【解决方案5】:

      我建议逻辑反转:循环遍历RadioButtons,我们检测是否应该检查每个ReadioButton

        // providing that all the radio buttons are on the form;
        // if they are on, say, myPanel, put myPanel instead of "this"
        var rButtons = this
          .Controls
          .OfType<RadioButton>()
          .Where(item => item.Name.StartsWith("radioButton_PLCtoHMI_Bool"));
      
        foreach (var rb in rButtons) {
          int index;
      
          if (int.TryParse(rb.Name.SubString("radioButton_PLCtoHMI_Bool".Length), out index))
            if (index >= 0 && index < KepwarearrWordtoBools_PLCtoHMI.Length)
              rb.Checked = KepwarearrWordtoBools_PLCtoHMI[index];
        }
      

      【讨论】:

        猜你喜欢
        • 2020-03-13
        • 1970-01-01
        • 2014-01-01
        • 2016-06-26
        • 1970-01-01
        • 2016-08-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多