2010-04-22 114 views
1

我在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); 
} 

我在哪里犯那个错误?

+0

你试图调试应用程序。 RemoveByKey之前和之后的控件列表是什么? – RvdK 2010-04-22 07:27:48

回答

1

首先,tbMax的名称不是“tbMax”,而是“txtBoxNoiseMax”。因此,对于其中一个,它将无法找到RemoveByKey上的文本框。

您每次都在制作新的控件。

+0

Thx ...现在纠正错误:) – gsvirdi 2010-04-22 09:07:46

0

正如LC已经提到:

你命名你的文本框的变量tbMax,但你给它的名字txtBoxNoiseMax。如果您看一下RemoveByKey的描述,您会看到它在名称属性中起作用。所以,你应该改变

tbMax.Name = "txtBoxNoiseMax"; 

tbMax.Name = "tbMax"; 
相关问题