Click to See Complete Forum and Search --> : For each


kit951
May 22nd, 2002, 10:51 AM
Can I do a for each textbox on a form loop? I want to loop through all the textboxes on a form and change the background color if they are null.

Thanks!

Cakkie
May 22nd, 2002, 12:11 PM
You cannot do it directly, but you can enumerate each control on a form, check for the type to see if it's a textbox and do your stuff

Dim ctl As Control

For Each ctl In Me.Controls
If TypeOf ctl Is TextBox Then
If ctl.Text = "" Then
ctl.BackColor = Drawing.Color.Red
Else
ctl.BackColor = Drawing.Color.White
End If
End If
Next

kit951
May 22nd, 2002, 01:18 PM
I'm getting these errors:

error BC30456: 'Text' is not a member of 'System.Web.UI.Control'.
error BC30456: 'BackColor' is not a member of 'System.Web.UI.Control'.
error BC30456: 'BackColor' is not a member of 'System.Web.UI.Control'.

Can you help? Thanks!

Cakkie
May 22nd, 2002, 04:45 PM
My guess is that you are not building a a windows application, but a web application. The code I gave you works with forms, but appearantly not with web controls.

If you are building a web application, I'm affraid I can't help you.
If you are not building a web application, try to find out why you are using webcontrols on your form, and try changing the the line with the TypeOf keyword to:

If TypeOf ctl Is System.Windows.Forms.TextBox Then


Good luck

Futureit2000
October 9th, 2005, 10:31 AM
Ok i know this is a pretty old post but this is the solution i just found. Not very nice and clean but it does work!

Dim obj As Control
Dim obj2 As HtmlForm
Dim obj3 As Object

For Each obj In Me.Page.Controls
If TypeOf obj Is HtmlForm Then
For Each obj3 In obj.Controls
If TypeOf obj3 Is WebControls.TextBox Then
' Do what ever here!
End If
Next
End If
Next