代码之家  ›  专栏  ›  技术社区  ›  SimpleOne

从现有按钮创建按钮数组(集合)

  •  1
  • SimpleOne  · 技术社区  · 14 年前

    有没有一种简单的方法可以从窗体上现有的按钮创建按钮集合?(用C表示)。

    我的窗体上已经有一系列按钮,我想使用索引来访问它们…例如:

    myButtonArray[0].ForeColor ...// Do something with it
    

    能做到吗?

    编辑:我可以将数组设置为具有通用onclick事件吗?然后确定单击了数组中的哪个按钮,比如,更改了它的颜色?

    8 回复  |  直到 6 年前
        1
  •  1
  •   Jon Skeet    14 年前

    您可以使用与任何其他数组相同的方法来执行此操作。例如:

    Button[] array = { firstButton, secondButton };
    

    或者,如果您需要在一个地方申报并稍后分配:

    Button[] array;
    ...
    array = new Button[] { firstButton, secondButton };
    

    在C 3+中,可以对数组初始值设定项使用隐式类型:

    Button[] array;
    ...
    array = new[] { firstButton, secondButton };
    

    您也可以考虑使用 List<Button> 相反:

    List<Button> buttons = new List<Button> { firstButton, secondButton };
    
        2
  •  6
  •   Bryan    14 年前

    快去营救!!

    Button[] buttons = this.Controls.OfType<Button>().ToArray();
    
        3
  •  3
  •   KeithS    14 年前
    var myButtonArray = new [] {this.Button1, this.Button2, ...}
    

    若要简化此过程(如果有许多按钮),可以在表单级别尝试此代码:

    this.Controls.OfType<Button>().ToArray();
    

    您可以在控件集合中具有非空控件集合本身的任何控件中重复此操作。

        4
  •  1
  •   John Boker    14 年前

    比如:

    var myButtonArray = new[] {btn1, btn2, btn3, btn4};
    
        5
  •  1
  •   Edgar Hernandez    14 年前

    表单的Controls属性中包含所有控件,因此必须迭代该集合并将其添加到数组中。

    List<Button> buttons = new List<Button>();
    
    foreach(Control c in this.Controls)
    {
        Button b = c as Button;
        if(b != null)
        {
            buttons.Add(b);
        }
    }
    
        6
  •  1
  •   Marcello B.    6 年前

    如果您使用的是C 7.0或更高版本,您可以使用 is 关键字,用于在循环遍历每个控件时检查它们是否为按钮。

    List<Button> buttons = new List<Button>();//CREATE LIST FOR BUTTONS
    
    //LOOP THROUGH EACH CONTROL ON FORM
    foreach (Control c in Controls)
    {
        //IF THE CONTROL IS A BUTTON ADD IT TO THE LIST
        if (c is Button b)
        {
            buttons.Add(b);
        }
    }
    

    有关C的旧版本,请参阅@ Edgar Hernandez answer

        7
  •  0
  •   drooksy    14 年前

    假设存在命名约定…

    List<Button> asdf = new List<Button>();
    for (int x = 0; x <= 10; x++) {
        asdf.Add(myButton + x);
    }
    
        8
  •  0
  •   yonan2236    14 年前

    In response to your requirements : ( Edit: Can I set the array to have a generic OnClick event? And then determine which button in the array was clicked and, say, change its color? )

    List<Button> buttons = new List<Button> { firstButton, secondButton };
    
    // Iterate through the collection of Controls, or you can use your List of buttons above.
    foreach (Control button in this.Controls)
    {
        if (button.GetType() == typeof(Button)) // Check if the control is a Button.
        {
            Button btn = (Button)button; // Cast the control to Button.
            btn.Click += new System.EventHandler(this.button_Click); // Add event to button.
        }
    }
    
    // Click event for all Buttons control.
    private void button_Click(Button sender, EventArgs e) 
    {
        ChangeForeColor(sender); // A method that accepts a Button
        // other methods to do...
        // you can also check here what button is being clicked 
        // and what action to do for that particular button.
        // Ex:
        //
        // switch(sender.Name)
        // {
        //     case "firstButton":
        //         sender.ForeColor = Color.Blue;
        //         break;
        //     case "secondButton ":
        //         sender.Text = "I'm Button 2";
        //         break;
        // }
    }
    
    // Changes the ForeColor of the Button being passed.
    private void ChangeForeColor(Button btn)
    {
        btn.ForeColor = Color.Red;
    }