C# 如何删除 switch 中动态创建的控件
C# How to remove dynamically created controls in switch
好的,所以在我的 switch(comboBox1.SelectedIndex) 中 case 1 它会动态创建一些标签和组合框并添加它们到 tabPage1,但我希望在选择 case 2
时删除那些动态创建的控件
public void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
switch (comboBox2.SelectedIndex)
{
case 0:
{
//do nothing.
break;
}
case 1:
{
Label label16 = new Label();
tabPage1.Controls.Add(label16);
label16.Left = 465;
label16.Top = 111;
label16.Text = "Time:";
label16.Size = new Size(60, 13);
ComboBox comboBox13 = new ComboBox();
tabPage1.Controls.Add(comboBox13);
comboBox13.Left = 533;
comboBox13.Top = 108;
comboBox13.Size = new Size(104, 21);
comboBox13.DropDownStyle = ComboBoxStyle.DropDownList;
comboBox13.DisplayMember = "Text";
comboBox13.ValueMember = "Value";
var ComboBox13Items = new[] {
new { Text = "1 Second", Value = "1" },
new { Text = "2.5 Seconds", Value = "2.5" },
new { Text = "5 Seconds", Value = "5" },
new { Text = "7.5 Seconds", Value = "7.5" },
new { Text = "10 Seconds", Value = "10" }
};
comboBox13.DataSource = ComboBox13Items;
break;
}
case 2:
{
foreach (Control TimeLabel in tabPage1.Controls.OfType<Controls>())
{
if (TimeLabel.Name == "label16")
tabPage1.Controls.Remove(TimeLabel);
}
foreach (Control TimeComboBox in tabPage1.Controls.OfType<Controls>())
{
if (TimeComboBox.Name == "comboBox13")
tabPage1.Controls.Remove(TimeComboBox);
}
break;
}
我也尝试将 OfType<Controls>
更改为 OfType<Label>
和 OfType<ComboBox>
,仍然没有成功:/
Label label16 = new Label();
tabPage1.Controls.Add(label16);
label16.Left = 465;
label16.Top = 111;
label16.Text = "Time:";
label16.Size = new Size(60, 13);
不创建名为 "label16" 的按钮,而是创建一个未命名的按钮。
您需要添加
labal16.Name = "label16";
名称应该是唯一的,如果有可能添加不止一组,请保留一个计数器或其他东西,并使用计数器来制作一个唯一的名称。
好的,所以在我的 switch(comboBox1.SelectedIndex) 中 case 1 它会动态创建一些标签和组合框并添加它们到 tabPage1,但我希望在选择 case 2
时删除那些动态创建的控件public void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
switch (comboBox2.SelectedIndex)
{
case 0:
{
//do nothing.
break;
}
case 1:
{
Label label16 = new Label();
tabPage1.Controls.Add(label16);
label16.Left = 465;
label16.Top = 111;
label16.Text = "Time:";
label16.Size = new Size(60, 13);
ComboBox comboBox13 = new ComboBox();
tabPage1.Controls.Add(comboBox13);
comboBox13.Left = 533;
comboBox13.Top = 108;
comboBox13.Size = new Size(104, 21);
comboBox13.DropDownStyle = ComboBoxStyle.DropDownList;
comboBox13.DisplayMember = "Text";
comboBox13.ValueMember = "Value";
var ComboBox13Items = new[] {
new { Text = "1 Second", Value = "1" },
new { Text = "2.5 Seconds", Value = "2.5" },
new { Text = "5 Seconds", Value = "5" },
new { Text = "7.5 Seconds", Value = "7.5" },
new { Text = "10 Seconds", Value = "10" }
};
comboBox13.DataSource = ComboBox13Items;
break;
}
case 2:
{
foreach (Control TimeLabel in tabPage1.Controls.OfType<Controls>())
{
if (TimeLabel.Name == "label16")
tabPage1.Controls.Remove(TimeLabel);
}
foreach (Control TimeComboBox in tabPage1.Controls.OfType<Controls>())
{
if (TimeComboBox.Name == "comboBox13")
tabPage1.Controls.Remove(TimeComboBox);
}
break;
}
我也尝试将 OfType<Controls>
更改为 OfType<Label>
和 OfType<ComboBox>
,仍然没有成功:/
Label label16 = new Label();
tabPage1.Controls.Add(label16);
label16.Left = 465;
label16.Top = 111;
label16.Text = "Time:";
label16.Size = new Size(60, 13);
不创建名为 "label16" 的按钮,而是创建一个未命名的按钮。
您需要添加
labal16.Name = "label16";
名称应该是唯一的,如果有可能添加不止一组,请保留一个计数器或其他东西,并使用计数器来制作一个唯一的名称。