简体   繁体   English

一些Windows窗体控件无法识别?

[英]some windows form controls not recognized?

I have aa couple of ordinary Windows Forms. 我有几个普通的Windows窗体。 To clear the data entry fields in one of them I use code something like this: 为了清除其中之一的数据输入字段,我使用如下代码:

    ClearInputFields(this);

    private void ClearInputFields(Control Page)
    {
        try
        {
            foreach (Control ctrl in Page.Controls)
            {
                if (ctrl is Button)                    
                    continue;
                if (ctrl is DataGridView)
                    continue;
                if (ctrl is ListBox)
                    continue;

                if (ctrl is TextBox)
                {
                    ((TextBox)(ctrl)).Text = string.Empty;
                }
                else if (ctrl is ComboBox)
                {        
                    ((ComboBox)(ctrl)).SelectedIndex = 0;
                }
                else if (ctrl is CheckBox)
                {
                    ((CheckBox)(ctrl)).Checked = false;
                }
                else if (ctrl.Controls.Count > 0)
                {
                    ClearInputFields(ctrl);
                }                                       
            }
        }
        catch (Exception ex)
        {
            TraceFile.Error("ExceptionLog", ex);
        }
    }

On one of the forms it works as expected. 在其中一种形式上,它可以按预期工作。 On the other form two out of nine ComboBoxs are NOT reset, and none of the six CheckBoxes are cleared. 在另一种形式上,不重置九个组合框中的两个,并且不清除六个复选框中的任何一个。

Stepping through in the debugger, it appears that the particular controls are not even in the list of controls contained in the form.?! 逐步进入调试器,似乎特定控件甚至不在窗体中包含的控件列表中。

I have compared the forms, and see nothing different in the properties of these controls. 我已经比较了表格,发现这些控件的属性没有什么不同。 One of the comboBoxes that is not recognized is databound, one is not, but this is also true in the case of those that are reset. 不能识别的comboBox之一是数据绑定的,不是的,但是在重置的情况下也是如此。

I have looked at the designer, and all controls are declared as "this.Controls.Add(this.name_of_control);" 我看过设计器,所有控件都声明为“ this.Controls.Add(this.name_of_control);”。

I have tried leaving out the last "else if" (there are no panels, group boxes, or other containers on this form anyway). 我尝试过省略最后一个“否则”(无论此表单上没有面板,组框或其他容器)。 No difference in behavior. 行为无差异。

If I explicitly set the Checkboxes to "Checked = false", and set the ComboBoxes to "SelectedIndex = 0" they do what I expect, but not when iterating through the controls of the form. 如果我将Checkboxes显式设置为“ Checked = false”,并将ComboBoxes设置为“ SelectedIndex = 0”,它们将执行我期望的操作,但在遍历窗体的控件时则不会。

EDIT - Addendum: All controls are created in the Designer, not at runtime. 编辑-附录:所有控件都在设计器中创建,而不是在运行时创建。 As mentioned, there are no containers on the form. 如前所述,表单上没有容器。

I expect this is one of those simple, obvious, right under your nose issues, but so far I'm not seeing it. 我希望这是那些简单,显而易见的问题,就在您的鼻子下面,但是到目前为止,我还没有看到它。

Any insights appreciated. 任何见解表示赞赏。 Thanks! 谢谢!

Perhaps when changing a control's property – the order of the controls in this.Controls changes, so you're not going through all controls. 也许在更改控件的属性时– this.Controls控件的顺序会更改,因此您无需遍历所有控件。 Although maybe this is not the case because maybe the Control ctrl in Page.Controls is checked only the first time. 尽管可能不是这样,因为Control ctrl in Page.ControlsControl ctrl in Page.Controls可能仅是第一次检查。

Whenever I have to do something on the items of a Collection that could alter it, I usually copy the collection and then loop through this copy. 每当我必须对某个集合的项目执行可能会更改它的操作时,我通常都会复制该集合,然后遍历此副本。 In your case this is the code that I usually write: 在您的情况下,这是我通常编写的代码:

ClearInputFields(this);

private List<Control> ControlsForPage(Control Page)
{
        List<Control> result = new List<Control>();
        foreach (Control ctrl in Page.Controls)
              result.Add(ctrl);
        return result;
{

private void ClearInputFields(Control Page)
{
    try
    {
        List<Control> ctrlsCopy = ControlsForPage(Page);
        foreach (Control ctrl in ctrlsCopy)
        {
            if (ctrl is Button)                    
                continue;
            if (ctrl is DataGridView)
                continue;
            if (ctrl is ListBox)
                continue;

            if (ctrl is TextBox)
            {
                ((TextBox)(ctrl)).Text = string.Empty;
            }
            else if (ctrl is ComboBox)
            {        
                ((ComboBox)(ctrl)).SelectedIndex = 0;
            }
            else if (ctrl is CheckBox)
            {
                ((CheckBox)(ctrl)).Checked = false;
            }
            else if (ctrl.Controls.Count > 0)
            {
                ClearInputFields(ctrl);
            }                                       
        }
    }
    catch (Exception ex)
    {
        TraceFile.Error("ExceptionLog", ex);
    }
}

Anyway, just to understand what's going on here, I would try to make the copy of Controls before doing your reset, and then compare it with the Controls collection of your page after the reset. 无论如何,为了了解这里发生的情况,我将尝试在进行重置之前制作控件的副本,然后将其与重置后的页面的Controls集合进行比较。

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

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