我在C#窗體窗體應用程序中工作,我在其中添加3個具有相同名稱的不同控件(一個按鈕,一個文本框&一個標籤)到我的窗體。在C#窗口中添加具有相同名稱的不同控件
爲什麼button4_Click中有錯誤?
CODE:
private void button1_Click(object sender, EventArgs e)
{
TextBox myControl = new TextBox();
myControl.Name = "myControl";
this.Controls.Add(myControl);
}
private void button2_Click(object sender, EventArgs e)
{
Button myControl = new Button();
myControl.Name = "myControl";
this.Controls.Add(myControl);
}
private void button3_Click(object sender, EventArgs e)
{
Label myControl = new Label();
myControl.Name = "myControl";
this.Controls.Add(myControl);
}
private void button4_Click(object sender, EventArgs e)
{
((ComboBox)this.Controls["myControl"]).Text = "myCombo"; // works
((TextBox)this.Controls["myControl"]).Text = "myText"; // error
((Label)this.Controls["myControl"]).Text = "myLabel"; // error
}
你應該命名你的按鈕。 – SLaks 2010-11-28 18:36:50