【问题标题】:How can i foreach all buttons in winforms (C#)我如何在winforms(C#)中foreach所有按钮
【发布时间】:2014-01-26 10:01:07
【问题描述】:

请问您能帮帮我吗?我在 winforms.designer.cs 中有所有按钮,并且我为所有按钮分配了相同的处理程序。处理程序是MouseEnterMouseLeave。我需要找到所有按钮并为每个按钮分配不同的MouseEnterMouseLeave

我在一个按钮上试过这个,但它不起作用。

private void createButton_MouseEnter(object sender, EventArgs e)
{
    createButton.Cursor = NativeMethods.LoadCustomCursor(Path.Combine(collection.source, collection.cursor_hand));
    switch (Name)
    {
        case "createButton":
            this.createButton.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.create_on));
            break;
    }
}

【问题讨论】:

  • 尽量清楚一点。你想分配什么?你有错误吗?
  • 我没有任何错误。我想按名称搜索所有按钮。我加了一键切换,但他不叫。我不知道为什么。

标签: c# winforms button case


【解决方案1】:

您可以使用OfType 扩展方法轻松循环所有按钮,如下所示:

foreach(var button in this.Controls.OfType<Button>())
{
    button.MouseEnter += createButton_MouseEnter;
    button.MouseLeave += createButton_MouseEnter;
}

如果您想在您的createButton_MouseEnter 中获取当前的Button's Name,您可以执行以下操作:

private void createButton_MouseEnter(object sender, EventArgs e)
{
   var currentButton = sender as Button;
   var name = currentButton.Name;
   ...
}

【讨论】:

    【解决方案2】:

    您可以通过这种方式访问​​ winfrom 上的所有按钮:

      foreach (var control in this.Controls)
             {
                 if (control.GetType() == typeof(Button))
                 {
    
                     //do stuff with control in form
                 }
             }
    

    在任何事件处理程序后面使用此代码来循环遍历 winform 中的所有控件。谢谢

    【讨论】:

      【解决方案3】:

      您也许应该考虑创建一个递归方法,因为您可能有一个带有按钮的面板。

      所以在你的表单加载中你可以去:

      foreach(Control control in form.Controls)
      {
             LoopControls(control);
      }
      

      你可以像这样用你的按钮“玩”,因为它现在是你的情况下的一个变量 我会添加一些其他控件的示例,供一些想要做类似的人使用

      static void LoopControls(Control control)
      {
          switch(control)
          {
              case Button button:
                  if(button.Name.Equals("createButton",StringComparison.Ordinal)
                  button.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.create_on));
      
                  // other stuf if you need to...
                  break;
              case ListView listView:
                  // other stuf if you need to...
                  break;
              case Label label:
                  // other stuf if you need to...                  
                  break;
              case Panel panel:
                  // other stuf if you need to...
                  break;
              case TabControl tabcontrol:
                  // other stuf if you need to...
                  break;
              case PropertyGrid propertyGrid:
                  // other stuf if you need to...
                  break;
          }
          foreach(Control child in control.Controls)
              LoopControls(child);
      }
      

      【讨论】:

        【解决方案4】:

        我认为这很简单,如果我没有理解这个问题,我很抱歉:

        if (sender == createButton)
        {
            createButton.Cursor = NativeMethods.LoadCustomCursor(Path.Combine(collection.source, collection.cursor_hand));
            createButton.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.create_on));
        }
         if (sender == otherButton)
         {
             //otherCode
         }
        

        【讨论】:

          猜你喜欢
          • 2019-12-01
          • 2021-07-03
          • 2018-12-15
          • 2018-07-25
          • 1970-01-01
          • 2010-09-21
          • 1970-01-01
          • 2022-12-08
          • 1970-01-01
          相关资源
          最近更新 更多