ここで説明されているabatishchevソリューションを使用して、すべての TextBox を反復処理できます。
私はこれについて彼を暗唱しています:
拡張メソッドを定義します。
public static IEnumerable<TControl> GetChildControls(this Control control) where TControl : Control
{
var children = (control.Controls != null) ? control.Controls.OfType<TControl>() : Enumerable.Empty<TControl>();
return children.SelectMany(c => GetChildControls(c)).Concat(children);
}
次に、次のように使用します。
var allTextBoxes = this.GetChildControls<TextBox>();
そして最後にそれらをループします:
foreach (TextBox tb in this.GetChildControls<TextBox>())
{
if(String.IsNullOrEmpty(tb.Text)
{
// add textbox name/Id to array
}
}
すべての TextBox Id をコレクションに追加し、最後にこのコレクションを使用して、入力する必要がある Textboex をユーザーに示すことができます。
編集:
foreach ループは少し誤解を招く
次のように使用できます。
foreach (TextBox tb in this.GetChildControls<TextBox>()) { ... }
また
foreach (TextBox tb in allTextBoxes) { ... }
事前に変数に保存している場合。