【问题标题】:Add an OnLeave function to 140 objects?向 140 个对象添加 OnLeave 函数?
【发布时间】:2014-08-09 18:54:23
【问题描述】:

我得到了一个包含大约 140 个 numericUpDown 元素的 windows 窗体,并希望所有这些元素都这样做:

private void numericUpDown_B1_RS_LS_Leave(object sender, EventArgs e)
{
    if (numericUpDown_B1_RS_LS.Value < 1 || numericUpDown_B1_RS_LS.Value > 6)
    {
        numericUpDown_B1_RS_LS.BackColor = Color.Red;
    }
    else
    {
        numericUpDown_B1_RS_LS.BackColor = Color.White;
    }
}

有没有比手动向表单添加 140 个函数更舒适的方法?

【问题讨论】:

    标签: c# winforms forms visual-studio-2010 visual-studio


    【解决方案1】:

    假设您希望表单上的所有 NUD 都使用相同的方法,则可以这样做:

    public Form1()
    {
        InitializeComponent();
        foreach (Control ctl in this.Controls)
            if (ctl.GetType() == typeof(NumericUpDown) ) ctl.Leave += numericUpDown_Leave;
    
    }
    
    
    private void numericUpDown_Leave(object sender, EventArgs e)
        {
            NumericUpDown NumericUD =  (NumericUpDown) sender ;
            if (NumericUD.Value < 1 || NumericUD.Value > 6)
            {
                NumericUD.BackColor = Color.Red;
            }
            else
            {
                NumericUD.BackColor = Color.White;
            }
        }
    

    如果 NUD 不直接位于 Form 上,您可以轻松地遍历另一个容器的 Controls 集合,例如 Panel 或 GroupBox。如果某些 NUD 应该被排除在该行为之外,您可以以某种方式标记它们,可能在它们的标签或名称中并在添加它们的处理程序之前检查它..

    【讨论】:

    • 工作,除了 if 子句需要:ctl.GetType() == typeof(NumericUpDown)
    • 啊,对,对不起!我想知道为什么我总是想念它以及为什么它在一小时前没有出现在编译器中 - 它现在确实出现了,当然.. ;-) 我已经更正了答案。
    【解决方案2】:

    您不应该为相同的代码编写多个函数。相反,您可以只创建一个函数并将其分配给 numericUpDown 元素的事件方法,即“UpDown”事件。

    因此,无论何时触发任何元素的“UpDown”事件,都会执行相同的函数。至于你想为不同的元素编写不同的方法,那么在你的方法中考虑一下

    Method(object sender, EventArguments e)
    

    sender 是您的 updownElement 类型的实际发送者对象,并且该代码将仅针对该特定对象执行。

    您可以使用 for 循环来迭代所有 140 个元素并将此函数分配给“UpDown”元素。

    【讨论】:

      【解决方案3】:

      也许您可以编写一个静态方法来递归处理添加事件处理程序,例如:

          private void frmMain_Load(object sender, EventArgs e)
          { 
               AddHandleNumericUpdownLeave(this);
          }
      
          public static void AddHandleNumericUpdownLeave(Control theContrl)
          {
              if (theContrl.Controls != null && theContrl.Controls.Count > 0)
              {
                  foreach (Control cControl in theContrl.Controls)
                  {
                      AddHandleNumericUpdownLeave(cControl);
                  }
              }
              else
              {
                  NumericUpDown nudCtrl = theContrl as NumericUpDown;
                  if (nudCtrl != null)
                  {
                      nudCtrl.Leave += (object senderT, EventArgs eT) =>
                      {
                          var tmpCtrl = senderT as NumericUpDown;
                          if (tmpCtrl != null)
                          {
                              if (tmpCtrl.Value < 1 || tmpCtrl.Value > 6)
                              {
                                  tmpCtrl.BackColor = Color.Red;
                              }
                              else
                              {
                                  tmpCtrl.BackColor = Color.White;
                              }
                          }
                      }
                  }
              }
          }
      

      但如果表单中有太多控件,这可能会很昂贵...

      【讨论】:

        猜你喜欢
        • 2013-06-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-13
        • 2016-03-17
        • 1970-01-01
        • 2022-12-06
        • 1970-01-01
        • 2022-06-17
        相关资源
        最近更新 更多