CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: For each

  1. #1
    Join Date
    Feb 2002
    Posts
    15

    Question For each

    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!

  2. #2
    Join Date
    Jan 2000
    Location
    Olen, Belgium
    Posts
    2,477
    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
    Tom Cannaerts
    email: [email protected]
    www.tom.be (dutch site)

  3. #3
    Join Date
    Feb 2002
    Posts
    15

    For Each

    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!

  4. #4
    Join Date
    Jan 2000
    Location
    Olen, Belgium
    Posts
    2,477
    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:
    Code:
    If TypeOf ctl Is System.Windows.Forms.TextBox Then
    Good luck
    Tom Cannaerts
    email: [email protected]
    www.tom.be (dutch site)

  5. #5
    Join Date
    Oct 2005
    Posts
    1

    Re: For each

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured