Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Searching by control type?

Status
Not open for further replies.

gudisdum

Programmer
Jun 13, 2003
13
NL
We are working on a form where we need to do several functions with all the text boxes on it.(clear, enable, disable..etc) I have set a for each loop that cycles through the control tags, then performs the function. This seems to be a work around to me. Does anyone know of a way to "For each" just the text boxes out of the control collection?

Thanks for the help.
 
This is a different solution but it doesn't only cycle through textboxes. I tried removing the IF.. statement and Dimmed ctrl as textbox but that didn't work.

Dim ctrl As Control
For Each ctrl In Me.Controls
If TypeOf (ctrl) Is System.Windows.Forms.TextBox Then
'....do something
End If
Next

Scott
Programmer Analyst
<{{><
 

You have to search your form for type text box by looping through the controls collection. But you also have to take into account container controls.(Group Boxes etc...)

This will clear all the textboxes on your form.

The Sub proceedure below will do what you need.
This is how you call it. Me references the form you are on.

ClearChildText(Me.Controls)


Private Sub ClearChildText(ByVal contin As Control.ControlCollection)



Dim foundcontrol As Control





For Each foundcontrol In contin

If foundcontrol.GetType.ToString = &quot;System.Windows.Forms.TextBox&quot; Then
'Use CType to convert object to textbox
CType(foundcontrol, TextBox).Clear()

End If



If foundcontrol.Controls.Count <> 0 Then

ClearChildText(foundcontrol.Controls)

End If

Next foundcontrol



End Sub




DotNetDoc
M.C.S.D.
---------------------------------------

Tell me and I forget. Show me and I remember. Involve me and I understand.
- Anonymous Chinese Proverb

-----------------------------------

If you can't explain it simply, you don't understand it well enough.
- A. Einstein





 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top