-
Feb 7th, 2014, 11:11 AM
#1
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
-
Mar 24th, 2014, 08:45 AM
#2
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.
-
Mar 24th, 2014, 09:39 AM
#3
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|