|
-
Mar 11th, 2011, 06:32 AM
#1
Thread Starter
Hyperactive Member
[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?
-
Mar 11th, 2011, 07:09 AM
#2
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.
-
Mar 11th, 2011, 08:27 AM
#3
Thread Starter
Hyperactive Member
Re: Validate TExtboxes
ValidateChildren does not work in VB.NET 2003
-
Mar 11th, 2011, 08:30 AM
#4
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|