![](/img/trans.png)
[英]How to get a list of all controls on WinForm even those in SplitContainers or Panels
[英]How to clear all SpellBox controls in WinForm
我有一個WinForm
項目,該項目利用了WPF
SpellBoxes
。 我現在正在創建一個函數來清除我的所有字段以及該類中的TextBoxes。 textBoxes
實際上是SpellBoxes
集成到項目中,這就是為什么我認為我遇到了這個問題。 以我的理解,這將是通過以下所有循環遍歷所有控件的最佳方式:
public void ClearControls()
{
foreach (Control control in panel1.Controls)
{
if (control is SpellBox)
{
SpellBox txt = (SpellBox)control;
txt.Text = "";
}
}
}
然后在點擊事件中調用ClearControls();
但是由於我使用SpellBoxes,我似乎甚至無法遍歷,就好像它們甚至未被識別。 上面的函數適用於TextBox的,但不適用於spellBox's
。 如果有人能告訴我為什么會這樣,我將不勝感激。 提前致謝
遞歸搜索的快速示例:
public void ClearControls(Control cntr)
{
foreach (Control control in cntr.Controls)
{
if (control is SpellBox)
{
control.Text = "";
}
else if(control.HasChildren)
{
ClearControls(control);
}
}
}
您可以通過經由窗體本身傳遞開始它關閉this
:
ClearControls(this);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.