【问题标题】:c# changing name of controls in for loopc#在for循环中更改控件的名称
【发布时间】:2016-01-08 01:18:05
【问题描述】:

我正在制作一个程序,该程序将通过 for 循环在屏幕上制作按钮。这很重要,因为用户需要访问按钮的数量,并且每次运行都会改变。这是我到目前为止的代码:

public Form1()
    {
        InitializeComponent();
        int top = 5;
        int left = 5;
        for (int i = 0; i < 10; i++)
        {
            Button button = new Button();
            button.Height = 50;
            button.Left = left;
            button.Top = top;
            this.Controls.Add(button);
            left += button.Width + 2;
        }
    }

我想要的基本上是这样的:

Button b+i = new Button();

就像组合两个字符串时,我希望循环中第一次运行的按钮名称为 b0,然后是 b1,然后是 b2,依此类推。

我使用 Visual Studio,InitializeComponent() 转到编辑器生成的代码来制作窗口和其他东西。除了窗户之外什么都没有。

感谢您的帮助!

【问题讨论】:

  • 你可以用button.Name来设置一个名字,比如string.Format("button{0}", i),也可以把i放在tag里面,在需要的时候使用。
  • 您不是在询问设置按钮的名称。您正在询问更改保存按钮实例的变量的名称,而您不能在运行时执行此操作。使用数组来包含按钮,然后你可以通过索引来引用它们。
  • 问题在 cmets 中有回答吗?
  • @RezaAghaei 是的,使用您、KenWhite 和 MitchWheat 提供的信息,我能够弄清楚。下面的链接还将用你们提供的概念来总结事情。 link 给未来有同样担忧的人

标签: c# winforms


【解决方案1】:

担心变量名是解决此问题的错误方法。有几种选择:

使用列表

List<Button> buttons;    

public Form1()
{
    InitializeComponent();
    buttons = new List<Button>();
    int top = 5;
    int left = 5;
    for (int i = 0; i < 10; i++)
    {
        Button button = new Button();
        button.Height = 50;
        button.Left = left;
        button.Top = top;
        this.Controls.Add(button);
        buttons.Add(button);
        left += button.Width + 2;
    }
}
//now instead of 'b1', it's 'buttons[1]'

您也可以使用 OfType() 方法创建:

var buttons = this.Controls.OfType<Button>().ToList();

这些可以结合 FlowLayoutPanel 或 TableLayoutPanel 来简化代码:

public Form1()
{
    InitializeComponent();

    for (int i = 0; i < 10; i++)
    {
        Button button = new Button();
        button.Height = 50;
        FlowLayoutPanel1.Controls.Add(button); //panel handles Left/Top location
    }
}

该面板还具有帮助您的应用在不同 dpi 或屏幕/窗口尺寸下扩展的优势。

其中任何一个也可以适应开始考虑连接到数据源(并减少代码):

var buttons = Enumerable.Range(0, 10).Select(b => new Button {
   Height = 50,
   Left = 5 + ( b * (BUTTONWIDTH + 2) ),
   Top = 5,
   Name = String.Format("Button{0}", b)
});

//buttons.ToList()
//or
//this.Controls.AddRange(buttons.ToArray())
//or 
//FlowLayoutPanel1.Controls.AddRange(buttons.ToArray()) //Remove Left/Top code

但所有这些都是毫无意义的,除非你可以让按钮真正做某事。您需要(至少)Click 事件的事件处理程序:

foreach (var b in butons)
{
     b.Click += (s,e) => 
    { 
        //click code for all of the buttons goes here
        // you can tell the buttons apart by looking at the "s" variable 

    };
}

【讨论】:

  • 两者都是合法的。在 docs page 上向下滚动对象和列表初始化程序,您将看到该示例。
  • 糟糕,认为只有在使用参数化构造函数时才合法。感谢您的澄清
  • 如果我可以为这个答案“投票”一千次。这太神奇了。你真了不起。谢谢你,先生。
【解决方案2】:

因为人们(正确地)说“使用数组”这里是代码

public Form1()
    {
        InitializeComponent();
        int top = 5;
        int left = 5;
        var buttons = new Button[10];
        for (int i = 0; i < 10; i++)
        {
            Button button = new Button();
            button.Height = 50;
            button.Left = left;
            button.Top = top;
            this.Controls.Add(button);
            left += button.Width + 2;
            buttons[i] = button;
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-08
    相关资源
    最近更新 更多