我在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