Results 1 to 3 of 3

Thread: Validating Controls

  1. #1

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,698

    Validating Controls

    I recently answered a thread where a guy wanted to know how to make sure that all textboxes had a value before moving on to preform some database operations. I suggested iterating through all the textboxes, use a conditional statement to check if the textbox has text, if not then exit the loop and let the user know. Another user suggested using a conditional statement on each individual textbox and let them know which textbox(or multiple textboxes) it was that had no value. This inspired me to write this snippet. Basically what it does is use the control's tag property to store which textbox it is that has missing values and then iterate through the collection of controls to check if there is a value or not. If there is no value then store it into an array or list. Finally let the user know which controls had an empty value. In theory this would work for just about any control(textbox, numericupdown, etc.) but in my example I'll just use textbox. I will also set all my controls in the form_load event to show you how I'm setting up the controls, but really it could be done in the designer view.

    Code:
    Option Strict On
    Option Explicit On
    Public Class Form1
    
        'Collection used to store the textboxes that require user input
        Private collection As New List(Of TextBox)
    
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            'Create some controls
            Dim fnameTextBox, lnameTextBox, phoneTextBox As New TextBox
            Dim fnameLabel, lnameLabel, phoneLabel As New Label
            Dim submitButton As New Button
    
            'Set their properties
            With fnameLabel
                .AutoSize = True
                .Left = 5
                .Text = "First Name:"
                .Top = 5
            End With
    
            With fnameTextBox
                .Left = fnameLabel.Right + 5
                .Name = "fnameTextBox"
                .Tag = "First Name"
                .Top = fnameLabel.Top
            End With
    
            With lnameLabel
                .AutoSize = True
                .Left = 5
                .Text = "Last Name:"
                .Top = fnameLabel.Bottom + 5
            End With
    
            With lnameTextBox
                .Left = lnameLabel.Right + 5
                .Name = "lnameTextBox"
                .Tag = "Last Name"
                .Top = lnameLabel.Top
            End With
    
            With phoneLabel
                .AutoSize = True
                .Left = 5
                .Text = "Phone #:"
                .Top = lnameLabel.Bottom + 5
            End With
    
            With phoneTextBox
                .Left = phoneLabel.Right + 5
                .Name = "phoneTextBox"
                .Tag = "Phone Number"
                .Top = phoneLabel.Top
            End With
    
            With submitButton
                .Left = phoneTextBox.Left
                .Name = "submitButton"
                .Text = "Submit"
                .Top = phoneTextBox.Bottom + 5
            End With
    
            'Add them to me
            Me.Controls.AddRange({fnameLabel, fnameTextBox, lnameLabel, lnameTextBox, phoneLabel, phoneTextBox, submitButton})
    
            'Add just the textboxes to the collection
            collection.AddRange({fnameTextBox, lnameTextBox, phoneTextBox})
    
            'Generate the click event
            AddHandler submitButton.Click, AddressOf submitButton_click
        End Sub
    
        Private Sub submitButton_click(sender As Object, e As EventArgs)
            'The list to store the values that were empty
            Dim invalid As New List(Of String)
    
            'Iterate through each control in the collection
            For Each txtbox As TextBox In collection
                'Check if the string is empty
                If String.IsNullOrWhiteSpace(txtbox.Text) Then
                    'If it is, add the tag to our list
                    invalid.Add(txtbox.Tag.ToString)
                End If
            Next
    
            'Check if the list of empty values was empty or not
            If invalid.Count > 0 Then
                Dim invalidStr As String = String.Empty
                'Loop through each item in our valid list
                For Each itm As String In invalid
                    If invalid.IndexOf(itm) = 0 Then
                        'If it's the first item, then don't add the , before adding the item
                        invalidStr &= itm
                    Else
                        invalidStr &= ", " & itm
                    End If
                Next
    
                'Let the user know what happened
                MessageBox.Show("Please enter data in for: " & invalidStr)
            Else
                'All textbox had text
            End If
        End Sub
    
    End Class
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  2. #2
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Validating Controls

    You know if you want to post a Form without attaching the project itself, you could have simply designed the Form as you normally would and just copy InitializeComponents from the designer file to the .vb file before posting it to the forum. It would save you the headache of having to hand code all those controls.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  3. #3

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,698

    Re: Validating Controls

    For some reason I've never thought of that. Most of the time if I want to show the user what controls I'm using, I'll spend about 10 - 15 minutes setting up all the controls in the form's load event like I did above.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

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