[英]How do i make an exception when clearing a c# form?
I need to clear a form of my application. 我需要清除我的申请表。 But there are 2 controls that I don't want it to clear.
但是,我不想清除2个控件。
When I click on another option in my ComboBox
. 当我在
ComboBox
单击另一个选项时。 It first wipes all controls and then loads the new controls in. (Or atleast that's what I'm trying to achieve.) 它首先擦除所有控件,然后将新控件加载到其中。(或者至少是我要实现的目标。)
This code loops through all controls within the form and checks whether the control names do not equal to cmbType
or lblType
. 此代码循环遍历表单中的所有控件,并检查控件名称是否等于
cmbType
或lblType
。 (Because this way it will leave those 2 controls untouched when clearing all controls.) (因为这样清除所有控件时,这两个控件将保持不变。)
foreach (Control c in this.Controls)
{
if (c.Name != "cmbType" && c.Name != "lblType")
{
this.Controls.Remove(c);
c.Dispose();
}
}
But this way it will sometimes when I click different options in my ComboBox
it will not remove all controls completely. 但是这样,有时当我在
ComboBox
单击其他选项时,它不会完全删除所有控件。 It will leave some controls which is not suppose to happen. 它将留下一些不应发生的控制。
列出
foreach (Control item in Controls.OfType<Control>().ToList())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.