In my last Blog entry I wrote about how to clear all text boxes in a windows form. But what if that form has tabs? Then you will need to set up a loop inside of loop inside of a loop. I know it seems confusing, but it is much better than the alternative of coding every text box name on every tab page into a reset or clear button. Especially if you have hundreds of text boxes. Here is the code to do it.
Dim x As Integer
For x = 0 To Me.Controls.Count - 1
If TypeOf Me.Controls.Item(x) Is TextBox Then
Me.Controls.Item(x).Text = ""
End If
If TypeOf Me.Controls.Item(x) Is TabControl Then
Dim tabControl As TabControl = Me.Controls.Item(x)
Dim z As Integer
For z = 0 To tabControl.TabCount() - 1
Dim TabPage As TabPage = tabControl.TabPages(z)
Dim y As Integer
For y = 0 To TabPage.Controls.Count - 1
If TypeOf TabPage.Controls.Item(y) Is TextBox Then
TabPage.Controls.Item(y).Text = ""
End If
Next
Next
End If
Next
We start out with a loop on the current page controls and clearing any text boxes there, then we detect if the control is a tab control. Then we start a loop inside of that for each tab page. Then a loop in that for each control on the tab page. I created variables with the name of the control to make it easier to read. You can leave this out and just use the name in the precdeing loop but it can get ugly, like: Me.Controls.Item(x).TabPages(z).Controls.Item(y).Text = "". This would be the end results if we did not you the variables.
You can use this same concept with any container on a form.