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!
Printable View
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!
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
Code: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
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!
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:
Good luckCode:If TypeOf ctl Is System.Windows.Forms.TextBox Then
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