Results 1 to 4 of 4

Thread: Code to check all text boxes

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2010
    Posts
    478

    Code to check all text boxes

    I used code below to check all text boxes but never work.
    All the textboxes directly on the form
    What is wrong? (I want to force user to input data into all text boxes)

    Private Sub ValidateTextBoxes()
    For Each ctrl As Control In Me.Controls
    If TypeOf ctrl Is TextBox Then
    Dim txtBox As TextBox = CType(ctrl, TextBox)
    If txtBox.Text.Length = 0 Then
    MsgBox("All fields are required")
    Exit Sub
    End If
    End If
    Next
    End Sub

    Hi, ChrisE ,
    Your code did not work.

    H, techgnome,
    Yes, All the textboxes directly on the form.

    For unknown reason, I can't post reply. (Company block reply but can use Edit)


    Here is message when I post reply

    Application Blocked!
    You have attempted to use an application which is in violation of your internet usage policy.
    vBulletin
    Category: Social.Media
    URL: http://www.vbforums.com/newreply.php...reply&t=858161
    Client IP: 192.168.4.171
    Server IP: 70.42.23.121
    User name: user
    Group name:
    Policy: a33a8d98-6b9a-51e5-511c-4b4aea8c1788
    FortiGate Hostname: FGT-300D-02
    Last edited by aspfun; Jan 23rd, 2018 at 10:32 AM.

  2. #2
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,048

    Re: Code to check all text boxes

    Hi,

    this will change the Background if the Textbox is empty...

    Code:
     Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            Call ColorTextboxIfEmpty(Me)
        End Sub
    
        Private Shared Sub ColorTextboxIfEmpty(ByRef frm As System.Windows.Forms.Form)
            For Each ctrl As Control In frm.Controls
                If TypeOf ctrl Is TextBox Then
                    If DirectCast(ctrl, TextBox).Text = String.Empty Then
                        ctrl.BackColor = Color.Tomato
                    End If
                End If
            Next
        End Sub
    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  3. #3
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: Code to check all text boxes

    Are all the textboxes directly on the form? Or do they reside in containers like a tab page, group box, or something like that? If that's the case, then the problem is that you're only iterating through the form's controls. After checking to see if it's a textbox, if it isn't, you need to check the .Controls property of that control, to see if there are any controls in it, and if so, process that group and so on and so on and on and on.... perfect time to learn about recurrsion.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,048

    Re: Code to check all text boxes

    Quote Originally Posted by aspfun View Post
    [COLOR="#FF0000"]Hi, ChrisE ,
    Your code did not work.
    then the Textbox isn't empty, it should work just fine, add a new Textbox to you form and try it again



    Tg- has a valid point if there are Panels ; Tabpages etc...


    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

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