Results 1 to 4 of 4

Thread: [RESOLVED] Validate TExtboxes

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    384

    Resolved [RESOLVED] Validate TExtboxes

    Hello.

    I have 200 dynamic textboxes.

    I am trying to limit the user input to exactly 5 characters ( not more, not less ), and numeric only.

    I have been trying this :
    Code:
       Private Function txtValid()
    
            Dim str As String
    
            For Each txt As TextBox In Panel1.Controls
                str = txt.Text
    
                If str.Length = 5 OrElse str = "" Then
                    If txt.Text.IndexOfAny(charactersAllowed) > -1 Then
                        txt.Text = lastText
    
                        Return True
                    Else
                        lastText = txt.Text
    
                        Return False
                    End If
                Else
                    Return False
                End If
    
    
            Next
    
    
        End Function
    Call it like :
    Code:
        Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
            If txtValid() Then
                dgSummary.PreferredColumnWidth = 240
                dgSummary.PreferredRowHeight = 25
    
                dt = GetProductSummary()
                dgSummary.DataSource = dt
    
                CopyAll(dgSummary)
    
                btnExecute.Enabled = True
    
            Else
                MessageBox.Show("Please enter FIVE ( 5 ) digits ( NUMBERS ) only!")
            End If
    But it always shows the last messagebox. Not all 200 textboxes will be filled in, so I suspect that it may have something to do with that, but I just need to stop the user from entering alphabetical characters, and the entered field must have a length of 5

    Can anyone help?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Validate TExtboxes

    You should be handling the Validating event of each TextBox. In the event handler, you test for an empty string or a length of 5 and use Double.TryParse to test for a number. If the data fails validation then you set e.Cancel to True and the control will not lose focus until the user corrects it. On your Button Click event, you call the ValidateChildren method of the form and it will raise the Validating event on every control, return False if any fail validation.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    384

    Re: Validate TExtboxes

    ValidateChildren does not work in VB.NET 2003

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    384

    Re: Validate TExtboxes

    I managed to get numbers only by doing this :

    Code:
                Dim txt2 As New TextBox
                AddHandler txt2.KeyPress, AddressOf txtNumbers
    
        Private Sub txtNumbers(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
            Dim tb As TextBox = sender
            If Not (Char.IsDigit(e.KeyChar) Or Char.IsControl(e.KeyChar)) Then
                e.Handled = True
            End If
        End Sub
    Let me see if I can limit the chars to 5...

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