如何通过传递表单上所有控件的方法更新 ToolStripStatusLabel 文本
How to update ToolStripStatusLabel Text from a method passing all the controls on the form
我有一种方法可以重置表单上的某些控件。除了名为 "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;
}
}
}
是因为:https://msdn.microsoft.com/en-us/library/system.windows.forms.toolstrip.controls(v=vs.110).aspx
Controls
集合未在 ToolStrip
和 ToolStripContainer
上使用。您应该改用 Items
:https://msdn.microsoft.com/en-us/library/system.windows.forms.toolstrip.items(v=vs.110).aspx
工具条上的项目不是 Control
对象。它们是 ToolStripItem
个对象。递归循环遍历 Controls
集合不会让您访问这些项目。
我有一种方法可以重置表单上的某些控件。除了名为 "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;
}
}
}
是因为:https://msdn.microsoft.com/en-us/library/system.windows.forms.toolstrip.controls(v=vs.110).aspx
Controls
集合未在 ToolStrip
和 ToolStripContainer
上使用。您应该改用 Items
:https://msdn.microsoft.com/en-us/library/system.windows.forms.toolstrip.items(v=vs.110).aspx
工具条上的项目不是 Control
对象。它们是 ToolStripItem
个对象。递归循环遍历 Controls
集合不会让您访问这些项目。