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

发布于 2024-09-29 05:38:29 字数 243 浏览 3 评论 0原文

有没有一种简单的方法可以从表单上的现有按钮创建按钮集合? (在 c# 中)。

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

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

可以这样做吗?

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

is there a simple way of creating a button collection from the existing buttons on my form? (In c#).

I have a series of buttons already on my form and I want to use an index to access them...e.g.:

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

Can this be done?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

薯片软お妹 2024-10-06 05:38:29

LINQ 来救援!

Button[] buttons = this.Controls.OfType<Button>().ToArray();

LINQ to the rescue!!

Button[] buttons = this.Controls.OfType<Button>().ToArray();
梦里的微风 2024-10-06 05:38:29

您可以按照与任何其他数组相同的方式进行操作。例如:

Button[] array = { firstButton, secondButton };

或者如果您需要在一处声明并稍后分配:

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

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

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

您可能还需要考虑使用 List

List<Button> buttons = new List<Button> { firstButton, secondButton };

You can do it the same way as for any other array. For example:

Button[] array = { firstButton, secondButton };

or if you need to declare in one place and assign later:

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

In C# 3+ you can use implicit typing for array initializers:

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

You might also want to consider using a List<Button> instead:

List<Button> buttons = new List<Button> { firstButton, secondButton };
与风相奔跑 2024-10-06 05:38:29
var myButtonArray = new [] {this.Button1, this.Button2, ...}

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

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

您可以使用 Controls 集合中具有非空 Controls 集合本身的任何 Control 来递归此代码。

var myButtonArray = new [] {this.Button1, this.Button2, ...}

To streamline this process if there are a lot of Buttons, you could try this code at the form level:

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

You could recurse this with any Control in the Controls collection that has a nonempty Controls collection itself.

愛放△進行李 2024-10-06 05:38:29

像这样的东西:

var myButtonArray = new[] {btn1, btn2, btn3, btn4};

something like:

var myButtonArray = new[] {btn1, btn2, btn3, btn4};
月光色 2024-10-06 05:38:29

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

List<Button> buttons = new List<Button>();

foreach(Control c in this.Controls)
{
    Button b = c as Button;
    if(b != null)
    {
        buttons.Add(b);
    }
}

You have all your controls in the Controls property of your form, so you have to iterate that collection and add it to your array.

List<Button> buttons = new List<Button>();

foreach(Control c in this.Controls)
{
    Button b = c as Button;
    if(b != null)
    {
        buttons.Add(b);
    }
}
━╋う一瞬間旳綻放 2024-10-06 05:38:29

响应您的要求
(编辑:我可以将数组设置为具有通用 OnClick 事件吗?然后确定单击了数组中的哪个按钮,然后更改它的颜色?)

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

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;
}
小霸王臭丫头 2024-10-06 05:38:29

如果您使用的是 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答案

If you are using C# 7.0 or higher you can use the is keyword to check if each control is a button as you loop through them.

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

For older versions of C# please see @Edgar Hernandez's answer

假设有命名约定...

List<Button> asdf = new List<Button>();
for (int x = 0; x <= 10; x++) {
    asdf.Add(myButton + x);
}

Assuming there is a naming convention...

List<Button> asdf = new List<Button>();
for (int x = 0; x <= 10; x++) {
    asdf.Add(myButton + x);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文