You need to enable JavaScript to run this app. 遍历所有Bunifu控件及Bunifu TextBox:原有窗体清空代码不生效问题咨询
优惠活动
大模型
产品
解决方案
定价
更多

遍历所有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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.11 15:27:03