简体   繁体   English

清除ac#表单时如何设置例外?

[英]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 . 此代码循环遍历表单中的所有控件,并检查控件名称是否等于cmbTypelblType (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.

相关问题 C#如何使图片框形成 - C# how do i make picturebox to form 为什么使用 Selenium C# 清除表单中的字段时,当我点击提交时,我的表单会重新填充数据? - Why when clearing the fields in my form with Selenium C# is my form repopulating with data when I hit submit? C#WinForms-如何制作锁定主表单的表单 - C# WinForms - How do I make a form that locks the main form 如何在不使用表单的情况下以C#在ASP.NET中创建消息表单 - How do I Make a message form in ASP.NET in C# without using a form C#:我如何理解Action之类的泛型 <IEnumerable<Customer> ,例外&gt; - C#: How do i make sense of generics like Action<IEnumerable<Customer>, Exception> 如何刷新c#中的表单? - How do I refresh a form in c#? 在C#中单击“计算”后,如何使表格中的文本框不可见? - How do I make a text box in a form not visible until after I click compute in C#? 当另一张表格关闭时,如何设置一张表格的标题(C#) - How do I set the caption of one form when another form closes (C#) 如何在 C# 中先加载一个表单? - How do I make one form load before the other in C#? 如何制作一个快速程序,该程序遍历C#Windows Form应用程序中的列表? - How do I make a quick program which goes through a list in a C# Windows Form Application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM