【发布时间】:2011-06-10 19:22:15
【问题描述】:
我想知道这在 c# winform 中是否可行。
按下按钮时创建控件并将其放置在给定位置。
我觉得可以这样
private TextBox txtBox = new TextBox();
private Button btnAdd = new Button();
private ListBox lstBox = new ListBox();
private CheckBox chkBox = new CheckBox();
private Label lblCount = new Label();
但问题出在每次按下按钮时都会创建同名控件。如何避免这种情况
什么大......
我写了,我期待它没有例外,因为控件已经包含btnAdd,而不是尽可能多的按钮创建你想要的。
访问它们会出现问题,但会通过@drachenstern 方法解决,对吗?
private void button1_Click_1(object sender, EventArgs e)
{
Button btnAdd = new Button();
btnAdd.BackColor = Color.Gray;
btnAdd.Text = "Add";
btnAdd.Location = new System.Drawing.Point(90, 25+i);
btnAdd.Size = new System.Drawing.Size(50, 25);
this.Controls.Add(btnAdd);
i = i + 10;
}
【问题讨论】: