如何在winforms(C#)中循环遍历所有按钮

3
请问能否帮我一下?我在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;
    }
}

请尽量更清晰明了。您想要分配什么?您是否遇到错误? - Abbas
我没有任何错误。我想通过名称搜索所有的按钮。我添加了一个按钮来切换,但它没有被调用。我不知道为什么。 - Radek Tarant
5个回答

18

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

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

在你的createButton_MouseEnter方法中,如果你想获取当前按钮的名称,你可以按照以下步骤进行:

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

3

在处理一个包含按钮的面板时,你可能需要考虑使用递归方法。

因此,在你的表单加载过程中,你可以这样做:

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

现在你可以像操作变量一样操作你的按钮,例如:

yourButton.Enabled = true; //启用按钮
yourButton.Text = "点击我"; //修改按钮文本

以下是其他控件的示例供有类似需求的人参考:

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

0
如果您想为多个表单创建一些常见的函数,那么您可以同时传递当前表单对象。
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;
         }
      }
   }
}

0

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

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
 }

0

您可以通过以下方式访问WinForm上的所有按钮:

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

使用此代码在任何事件处理程序中,循环遍历WinForm内的所有控件。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接