C# 使用基于int值的控件填充表单

C# 使用基于int值的控件填充表单,c#,winforms,C#,Winforms,我想知道如何着手做这样的事情: 我需要基于一个表示所需按钮数量的整数值创建一个具有特定按钮数量的表单,然后为它们指定自己的特定名称,这样每个按钮都可以有自己独特的事件处理程序 我能想到的一个真正的例子是Windows登录屏幕,其中创建的控件数量取决于用户数量以及是否有来宾帐户。你认为他们是怎么编程的 谢谢。for(int i=0;i

我想知道如何着手做这样的事情:

我需要基于一个表示所需按钮数量的整数值创建一个具有特定按钮数量的表单,然后为它们指定自己的特定名称,这样每个按钮都可以有自己独特的事件处理程序

我能想到的一个真正的例子是Windows登录屏幕,其中创建的控件数量取决于用户数量以及是否有来宾帐户。你认为他们是怎么编程的

谢谢。

for(int i=0;i<5;i++)
for (int i = 0; i < 5; i++)
{
  Button newButton = new Button();
  newButton.Name = "button" + i.ToString();
  newButton.Text = "Button #" + i.ToString();
  newButton.Location = new Point(32, i * 32);
  newButton.Click += new EventHandler(button1_Click);
  this.Controls.Add(newButton);
}

private void button1_Click(object sender, EventArgs e)
{
  if (((Button)sender).Name == "button0")
    MessageBox.Show("Button 0");
  else if (((Button)sender).Name == "button1")
    MessageBox.Show("Button 1");
}
{ 按钮newButton=新按钮(); newButton.Name=“button”+i.ToString(); newButton.Text=“Button#”+i.ToString(); 新按钮位置=新点(32,i*32); newButton.Click+=新建事件处理程序(button1_Click); this.Controls.Add(newButton); } 私有无效按钮1\u单击(对象发送者,事件参数e) { 如果(((按钮)发送者)。名称==“按钮0”) MessageBox.Show(“按钮0”); else if(((按钮)发送者)。Name==“button1”) MessageBox.Show(“按钮1”); }
用于(int i=0;i<5;i++)
{
按钮newButton=新按钮();
newButton.Name=“button”+i.ToString();
newButton.Text=“Button#”+i.ToString();
新按钮位置=新点(32,i*32);
newButton.Click+=新建事件处理程序(button1_Click);
this.Controls.Add(newButton);
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
如果(((按钮)发送者)。名称==“按钮0”)
MessageBox.Show(“按钮0”);
else if(((按钮)发送者)。Name==“button1”)
MessageBox.Show(“按钮1”);
}

无论如何,您必须定义所有按钮的名称。我建议您创建一个新的字符串数组,并在其中写入按钮名称,然后在按钮创建循环中使用它们:

//do the same length as the for loop below:
string[] buttonNames = { "button1", "button2", "button3", "button4", "button5" };

for (int i = 0; i < buttonNames.Lenght; i++)
{
  Button newButton = new Button();
  newButton.Name = "button" + i.ToString();
  newButton.Text = buttonNames[i]; //each button will now get its own name from array
  newButton.Location = new Point(32, i * 32);
  newbutton.Size = new Size(25,100); //maybe you can set different sizes too (especially for X axes)
  newButton.Click += new EventHandler(buttons_Click);
  this.Controls.Add(newButton);
}


private void buttons_Click(object sender, EventArgs e)
{
  Button btn = sender as Button
  MessageBox.Show("You clicked button: " + btn.Text + ".");
}
//执行与下面for循环相同的长度:
字符串[]buttonNames={“button1”、“button2”、“button3”、“button4”、“button5”};
对于(int i=0;i
无论如何,您必须定义所有按钮的名称。我建议您创建一个新的字符串数组,并在其中写入按钮名称,然后在按钮创建循环中使用它们:

//do the same length as the for loop below:
string[] buttonNames = { "button1", "button2", "button3", "button4", "button5" };

for (int i = 0; i < buttonNames.Lenght; i++)
{
  Button newButton = new Button();
  newButton.Name = "button" + i.ToString();
  newButton.Text = buttonNames[i]; //each button will now get its own name from array
  newButton.Location = new Point(32, i * 32);
  newbutton.Size = new Size(25,100); //maybe you can set different sizes too (especially for X axes)
  newButton.Click += new EventHandler(buttons_Click);
  this.Controls.Add(newButton);
}


private void buttons_Click(object sender, EventArgs e)
{
  Button btn = sender as Button
  MessageBox.Show("You clicked button: " + btn.Text + ".");
}
//执行与下面for循环相同的长度:
字符串[]buttonNames={“button1”、“button2”、“button3”、“button4”、“button5”};
对于(int i=0;i
如何定制每个按钮?使用newButton.Text=“foobar”会改变每个按钮,不是吗?@Jared无论您如何确定所需按钮的数量,您可能都应该有一个相应的名称列表,诸如此类。如何自定义每个按钮?使用newButton.Text=“foobar”会改变每个按钮,不是吗?@Jared无论您如何确定所需按钮的数量,您可能都应该有一个相应的名称列表等等。