• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 231
  • Last Modified:

Textbox Validation Or No validation

Hi,

I'm looking for a way do validation on 3 or many text boxes on a buttonclick

If all 3 text boxes   are blank, then that is okay.

If one or two of the text boxes is filled in and the others are empty, then this is an error condition.

Pls consider the answer assuming n number of textboxes .

Thx
0
sainavya1215
Asked:
sainavya1215
  • 6
  • 4
1 Solution
 
UncleMidriffCommented:
Try something like this:

Private Sub CheckTtextBoxes ()

Dim NumberOfTextBoxes as Integer = 0
Dim NumberBlank as Integer = 0
Dim ctl as Control

For Each ctl in Me.Controls
    If ctl.GetType().ToString = "System.Windows.Forms.TextBox" Then
        NumberOfTextBoxes = NumberOfTextBoxes + 1
        If ctl.text = "" then
            NumberBlank = NumberBlank + 1
        End if
    End If
Next ctl

If NumberBlank <> 0 AND NumberBlank < NumberOfTextBoxes then
    MsgBox("You Screwed up!")
End if

End Sub
0
 
matthersjrCommented:
How could this be done if on a child form?

Say the parent form has a button that opens a child form and the child form contains multiple text boxes that need validation.
0
 
UncleMidriffCommented:
As far as I know, each form has a Controls collection.  So instead of using "Me.Controls" in the above Sub, you could use something like "YourFormName.Controls" to get at the controls on YourForm.

Probably the easiest thing to do would be to include the above Sub on the child form and call it whenever the the textboxes on that child form need to be validated.  Or you could possibly change the Sub from "Private Sub CheckTextBoxes ()" to "Private Sub CheckTextBoxes (FormToCheck as Form)."  That way you could just write the Sub once and call it from wherever you need it.  

Please keep in mind that I haven't tested these ideas, so I can't say for certain that they are valid.  These are just the things that I would try first if I doing what you're asking about.

Hope that helps.

--UncleMidriff
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
matthersjrCommented:
I got it to work on the text box, but what if the text boxes are within a group box?
0
 
matthersjrCommented:
form 1 code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim secondForm As New Form2
        secondForm.ShowDialog()
    End Sub


form 2 code:

Private Sub CheckTtextBoxes()
        Dim NumberOfTextBoxes As Integer = 12 '12 textboxes within a group box
        Dim NumberBlank As Integer = 0
        Dim ctl As Control
        For Each ctl In Me.Controls
            If ctl.GetType().ToString = "System.Windows.Forms.TextBox" Then
                NumberOfTextBoxes = NumberOfTextBoxes + 1
                If ctl.Text = "" Then
                    NumberBlank = NumberBlank + 1
                End If
            End If
        Next ctl
        If NumberBlank <> 0 And NumberBlank < NumberOfTextBoxes Then
            MsgBox("You Screwed up!")
        End If
 End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        CheckTtextBoxes()
End Sub

Any idea?
0
 
matthersjrCommented:
For Each ctl In Me.GroupBox1.Controls

how would one set focus on the errored text box?
0
 
UncleMidriffCommented:
Would something like:

        For Each ctl In Me.Controls
            If ctl.GetType().ToString = "System.Windows.Forms.TextBox" Then
                NumberOfTextBoxes = NumberOfTextBoxes + 1
                If ctl.Text = "" Then
                    ctl.SetFocus()
                    Exit For  'I'm not sure is this is valid in VB.NET...it is in VB 6 though
                End If
            End If
        Next ctl

work?
0
 
matthersjrCommented:
I've modified it a bit.

Code for form 2:

Public Class Form2
    Inherits System.Windows.Forms.Form

Private blnOkay As Boolean = False

Private Sub CheckTtextBoxes()
        Dim NumberOfTextBoxes As Integer = 12
        Dim NumberBlank As Integer = 0
        Dim ctl As Control
        For Each ctl In Me.grpbxRainfallData.Controls
            If ctl.GetType().ToString = "System.Windows.Forms.TextBox" Then
                NumberOfTextBoxes = NumberOfTextBoxes + 1
                If Not IsNumeric(ctl.Text) Then
                    NumberBlank = NumberBlank + 1
                ElseIf ctl.Text < 0 Then
                    NumberBlank = NumberBlank + 1
                Else
                    '
                End If
            End If
        Next ctl
     If NumberBlank <> 0 And NumberBlank < NumberOfTextBoxes Then
            MsgBox("error")
            ctl.Focus() ' error here it drops to 3rd text box not the box in error????
            blnOkay = False
        Else
            blnOkay = True
        End If
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        CheckTtextBoxes()
        If blnOkay = True Then
            MsgBox("all good")
        End If
End Sub
End Class

So now the error is not focussing on the textbox that is errored.
0
 
matthersjrCommented:
                   NumberBlank = NumberBlank + 1
                    ctl.Select()

but it doesn't do the SelectAll()  
0
 
UncleMidriffCommented:
"...
If NumberBlank <> 0 And NumberBlank < NumberOfTextBoxes Then
            MsgBox("error")
            ctl.Focus() ' error here it drops to 3rd text box not the box in error????
            blnOkay = False
        Else
            blnOkay = True
        End If
..."

The reason it "drops to the third textbox" instead of the one you want it to drop to is because you are looping through all the controls on the form/groupbox, thus when the loop is finished, ctl will equal whatever control was hit last by the loop.  This is the reason that I had the "Exit For" in the code that I posted, so that when a blank textbox is found it'll set the focus to that textbox and stop looping through anymore controls.

"...
NumberBlank = NumberBlank + 1
                    ctl.Select()

but it doesn't do the SelectAll()
..."

I'm sure you realize this, but I'll say it just in case:  ctl.Select() isn't the same as ctl.SelectAll().  If you want to highlight some text in a texbox using textbox.Select(), you'll have to supply the start and length arguments, like textbox.Select(WhereToStartSelecting, HowManyCharToSelect).  If you want to highlight all the text in a textbox, it's better to use textbox.SelectAll().

Another thing to keep in mind:  If your textbox is blank, I'll bet textbox.SelectAll() will do nothing since there is no text in the textbox to select.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now