我在Windows窗體窗體中有三個選項卡。根據TabPages[0]
中選定的RadioButton,我在相關的TabPage上添加了幾個動態控件。在Button_Click
事件添加控件,但問題是我無法從其他(無關)TabPage刪除動態添加的控件。使用C#添加和刪除動態控件Windows窗體#
這裏是我的代碼:
Label label235 = new Label();
TextBox tbMax = new TextBox();
label235.Name = "label235";
tbMax.Name = "txtBoxNoiseMax";
label235.Text = "Noise";
tbMax.ReadOnly = true;
label235.ForeColor = System.Drawing.Color.Blue;
tbMax.BackColor = System.Drawing.Color.White;
label235.Size = new Size(74, 13);
tbMax.Size = new Size(85, 20);
if (radioButton1.Checked)
{
label235.Location = new Point(8, 476);
tbMax.Location = new Point(138, 473);
tabControl.TabPages[1].Controls.Add(label235);
tabControl.TabPages[1].Controls.Add(tbMax);
tabControl.TabPages[2].Controls.RemoveByKey("label235");
tabControl.TabPages[2].Controls.RemoveByKey("tbMax");
}
else
{
label235.Location = new Point(8, 538);
tbMax.Location = new Point(138, 535);
tabControl.TabPages[1].Controls.RemoveByKey("label235");
tabControl.TabPages[1].Controls.RemoveByKey("tbMax");
tabControl.TabPages[2].Controls.Add(label235);
tabControl.TabPages[2].Controls.Add(tbMax);
}
我在哪裏犯那個錯誤?
你試圖調試應用程序。 RemoveByKey之前和之後的控件列表是什麼? – RvdK 2010-04-22 07:27:48