Is there any way to for/each controls?

  • Thread starter Thread starter Terry Olsen
  • Start date Start date
T

Terry Olsen

Is there any way to run through all controls on a form using a For Each
loop? Say I want to clear all textboxes on a form...what's the best way to
do that?
 
Terry,

There is no best way.
My sample is this one.
\\\
doset(Me)
Private Sub doSet(ByVal parentCtr As Control)
Dim ctr As Control
For Each ctr In parentCtr.Controls
'DoSomething
doSet(ctr)
Next
End Sub
///
I take the start others take the child.

Be aware that you cannot use this to delete controls.
Than you can beter use a Z order loop.

I hope this helps,

Cor
 
Public Sub ClearAllChildFormText()
' Obtain a reference to the currently active MDI child form.
Dim tempChild As Form = Me.ActiveMdiChild

' Loop through all controls on the child form.
Dim i As Integer
For i = 0 To tempChild.Controls.Count - 1
' Determine if the current control on the child form is a TextBox.
If TypeOf tempChild.Controls(i) Is TextBox Then
' Clear the contents of the control since it is a TextBox.
tempChild.Controls(i).Text = ""
End If
Next i
End Sub 'ClearAllChildFormText
 
Back
Top