遍历所有Bunifu控件及Bunifu TextBox:原有窗体清空代码不生效问题咨询
问题根因
原有代码的判断逻辑仅匹配.NET原生的TextBox和ComboBox类型,而Bunifu输入控件为独立封装的自定义类型(如BunifuTextBox、BunifuDropdown等),无法命中原有判断条件,因此不会执行清空操作。
适配Bunifu控件的清空实现
public void Clear(Control c) { foreach (Control ct in c.Controls) { // 同时兼容原生控件和Bunifu常用输入控件 Type controlType = ct.GetType(); if (ct is TextBox || ct is ComboBox || controlType.FullName == "Bunifu.UI.WinForms.BunifuTextBox" || controlType.FullName == "Bunifu.UI.WinForms.BunifuDropdown" || controlType.FullName == "Bunifu.UI.WinForms.BunifuMaskedTextBox") { ct.Text = string.Empty; } // 递归遍历嵌套容器内的控件 if (ct.Controls.Count > 0) { Clear(ct); } } }
补充说明
- 若项目已引入Bunifu对应命名空间,可直接使用强类型判断,性能和准确性更高,判断逻辑示例:
if (ct is TextBox || ct is ComboBox || ct is BunifuTextBox || ct is BunifuDropdown || ct is BunifuMaskedTextBox) { ct.Text = string.Empty; }
- 如需清空其他Bunifu输入类控件(如
BunifuDatePicker、BunifuNumericUpDown等),直接在判断条件中追加对应类型即可 - 调用方式与原有逻辑一致,传入根容器控件即可清空该容器下所有符合条件的输入内容,例如传入当前窗体实例
Clear(this)即可清空整个窗体的输入内容
内容的提问来源于stack exchange,提问作者Mohammed El Attar
相关产品推荐
相关产品推荐

