简体   繁体   English

如何清除MDI子级的所有表单控件?

[英]How can I clear all form controls of an MDI child?

I have a function that clears a non-MDI child form, but when I apply it to an MDI child it doesn't do anything. 我有一个清除非MDI子窗体的函数,但是当我将其应用于MDI子窗体时,它什么也不做。 I've tried attacking this problem from a few different angles without any luck, was hoping stackoverflow could help! 我曾尝试从几个不同的角度来解决这个问题,但没有任何运气,希望stackoverflow可以帮助您!

I've tried the code shown, I've also attempted to reference the child of the parent of the current form (Which is circular I know). 我尝试了显示的代码,也尝试了引用当前表单的父级的子级(我知道这是圆形的)。

    public static void ResetAllControls(Control form)
    {
        foreach (Control control in form.Controls)
        {
            if (control is TextBox)
            {
                TextBox textBox = (TextBox)control;
                textBox.Text = "";
            }

            if (control is ComboBox)
            {
                ComboBox comboBox = (ComboBox)control;
                if (comboBox.Items.Count > 0)
                    comboBox.SelectedIndex = 0;
            }

            if (control is CheckBox)
            {
                CheckBox checkBox = (CheckBox)control;
                checkBox.Checked = true;
            }

            if (control is ListBox)
            {
                ListBox listBox = (ListBox)control;
                listBox.ClearSelected();
            }
        }
    }

I expect that passing this would clear all form controls as it does with standard forms. 我希望通过此操作将清除所有表单控件,就像处理标准表单一样。

I'm pretty sure that you have some kind of container on this for (eg. Panel) and controls are on this panel. 我很确定您在(例如面板)上有某种容器,并且控件在此面板上。 Form.Controls will give you only controls that lay directly on the form. Form.Controls将只为您提供直接位于窗体上的控件。 So you will have to do it recurrently: 因此,您将不得不经常执行此操作:

public static void ResetAllControls(Control parent)
{
    foreach(var child in parent.Controls)
        ResetAllControls(child);

    if(parent is TextBox)
    {
        (parent as TextBox).Text = "";
        return;
    }
    //and so on
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM