Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从传递表单上所有控件的方法更新ToolStripStatusLabel文本_C#_Statusstrip_Toolstripstatuslabel - Fatal编程技术网

C# 如何从传递表单上所有控件的方法更新ToolStripStatusLabel文本

C# 如何从传递表单上所有控件的方法更新ToolStripStatusLabel文本,c#,statusstrip,toolstripstatuslabel,C#,Statusstrip,Toolstripstatuslabel,我有一种方法可以重置窗体上的某些控件。除了名为tsStatusLabelInfo的ToolStripStatusLabel之外,我能够使该方法正常工作。此控件不会传递给resetForm方法 我相信它是StatusStrip控件的一部分,但我还没有弄清楚如何访问ToolStripStatusLabel控件来更新文本 private void resetButton_Click(object sender, EventArgs e) { Utilities.resetForm(this);

我有一种方法可以重置窗体上的某些控件。除了名为tsStatusLabelInfo的ToolStripStatusLabel之外,我能够使该方法正常工作。此控件不会传递给resetForm方法

我相信它是StatusStrip控件的一部分,但我还没有弄清楚如何访问ToolStripStatusLabel控件来更新文本

private void resetButton_Click(object sender, EventArgs e)
{
    Utilities.resetForm(this);
}

public static void resetForm(Control form)
{
    foreach(Control c in GetOffSprings(form))
    {
        if (c.Name == "folderTextBox")
        {
            ((TextBox)c).Clear();
        }
        else if (c.Name == "mfrListTextBox")
        {
            ((RichTextBox)c).Clear();
        }
        else if (c.Name == "mfrListDataGridView")
        {
            ((DataGridView)c).DataSource = null;
        }
        else if (c.Name == "onlyRadioButton")
        {
            ((RadioButton)c).Checked = true;
        }
        else if (c.Name == "usRadioButton")
        {
            ((RadioButton)c).Checked = true;
        }
        else if (c.Name == "otherYearsCheckedListBox")
        {
            ((CheckedListBox)c).SetItemCheckState(0, CheckState.Unchecked);
            ((CheckedListBox)c).SetItemCheckState(1, CheckState.Unchecked);
        }
        else if (c.Name == "yearComboBox")
        {
            ((ComboBox)c).Text = string.Empty;
        }
        else if (c.Name == "tsStatusLabelInfo")
        {
            //Control never pass
        }
        else if (c.Name == "statusStrip1")
        {
            // Exception:Object reference not set to an instance of an object
            ((StatusStrip)c).Controls["tsStatusLabelInfo"].Text = string.Empty;
        }
    }
}

//Loop through the control recursively getting all child controls
public static IEnumerable<Control> GetOffSprings(this Control @this)
{
    foreach(Control child in @this.Controls)
    {
        yield return child;

        //MessageBox.Show(child.Name);

        foreach (var offspring in GetOffSprings(child))
        {
            yield return offspring;
        }
    }
}
正因为如此:

控件集合未在ToolStrip和ToolStripContainer上使用。您应该使用以下项目:


工具条上的项目不是控制对象。它们是ToolStripItem对象。通过Controls集合递归循环将无法访问这些项。

确实如此-我将代码更改为StatusStripc.items[tsStatusLabelInfo].Text=string.Empty;谢谢你的帮助!!