Afn*_*hir 6 c# controls dynamic
我想知道在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();
Run Code Online (Sandbox Code Playgroud)
但是问题出在按下按钮时会创建相同的名称控件。
我写的是什么…… 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;
}
Run Code Online (Sandbox Code Playgroud)