繁体   English   中英

我如何 foreach winforms 中的所有按钮 (C#)

[英]How can i foreach all buttons in winforms (C#)

你能帮帮我吗? 我在 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;
    }
}

您可以使用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;
   ...
}

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

  foreach (var control in this.Controls)
         {
             if (control.GetType() == typeof(Button))
             {

                 //do stuff with control in form
             }
         }

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

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

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

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

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

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
 }

如果你想为多个 Forms 创建一些公共 function 那么你也可以通过当前表格 object

public class CommonHelper
{
   public void CreateButton_MouseEnter(Form currentForm)
   {
      var buttonsOnForm = currentForm.Controls.OfType<Button>();
      foreach (var btn in buttonsOnForm)
      {
         switch (btn.Name)
         {
             case "createButton":
             this.createButton.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.create_on));
             break;
         }
      }
   }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM