Visual C#2010:如何在运行时添加创建新标签或按钮,最后增加一个整数? [英] Visual C# 2010:How to add create new Labels or Buttons in Runtime with an incremented integer at the end?
问题描述
您好,
首先,我是业余程序员。
First of all, I am an amateur programmer.
您知道C#环境如何自动命名第一个按钮你拖动到窗体按钮1?然后当你拖动另一个时,它将其命名为button2。我不是在谈论文本,而是在谈论NAME。所以这没有特定的限制。我的问题是,我怎么做这个程序的b $ b?因此,每次单击按钮时,都应创建一个新按钮。如果我单击按钮5次,则应创建5个按钮。由于每个人都需要一个唯一的名称,最后的增量整数将是完美的,如下所示:
You know how the C# environment automatically names the first button you drag into the form as button1? Then when you drag another, it names it button2. I am not talking about the text, but the NAME. So this has no specified limit. My question is, how do I do that from the program? So each time I click a button, a new button should be created. If I click the button 5 times, 5 buttons should be created. As each one needs a unique name, an incrementing integer at the end would be perfect, like this:
namespace Duplicate
{
public partial class Form1 : Form
{
int c = 1;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
c++;
///Of course, this does not work:
Button "button"+ c = new Button();
}
}
}
我们可以吗?做类似的事情,至少是一些非常接近的事情?
Can we do something like that, at least something quite near?
提前致谢,
Aadi B。
推荐答案
而不是按钮 " button" + c
= new
按钮 (); 您应该创建一个新按钮并分配其属性,然后将其添加到以下形式的Controls集合中:
Instead of Button "button"+ c = new Button(); you should create a new button and assign its properties, then add it to the Controls collection in the form:
Button b = new Button();
b.Text = "button "+c;
b.Top = c*20; //20 pixels down from the previous button
this.Controls.Add(b);
这篇关于Visual C#2010:如何在运行时添加创建新标签或按钮,最后增加一个整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!