Is there any way to for/each controls?

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?
 
C

Cor Ligthert

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
 
S

Supra

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top