VB.net/フォーム内の全てのTextBoxを取得(再帰的)

getControlでコントロールを指定すると、そのコントロール内のTextBoxとComboBoxを全て返します。再帰的に探索しているので、TabPageなどの中の要素も全て返します。

[cc lang=”vb”]
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For Each control As Control In getControls(Me)
Console.WriteLine(control.Name && “:” && control.Text)
Next
End Sub

Private Function getControls(target As Control) As ArrayList
Dim controls As ArrayList = New ArrayList
For Each control As Control In target.Controls
If TypeOf control Is TextBox Then controls.Add(control)
If TypeOf control Is ComboBox Then controls.Add(control)
If control.HasChildren Then controls.AddRange(getControls(control))
Next
Return controls
End Function
[/cc]

jaJapanese