Click to See Complete Forum and Search --> : verifying the number of chars in a textbox


MonaVohra
03-14-2007, 08:22 PM
the textbox on my form should accept 6 or 7 chars/numbers. if it does not have 6 or 7, an error message shud be displayed. I have set the MaxLength property to 7. and I have written the following code:


If TextBox1.Text.Length <= 5 Then
MsgBox("can not be save")

End If

however it does not work. it still displays the error message if i enter 6 or 7 values

TwoFaced
03-14-2007, 09:02 PM
That should work just fine. Are you positive you are checking the correct textbox? Might the text really be in textbox2. I'd double check, because there is nothing wrong with it.

Phil Weber
03-15-2007, 02:47 AM
Where did you put that code? It should be in the textbox's Validating event.

MonaVohra
03-15-2007, 09:35 AM
I have a button called Next which leads to the next form. when the user enters text in the textbox and clicks on the Next button, the Next button chekcs the number of values in the textbox. if the user has not enter 6 or 7 values then the user cannot proceed to the next form and is therefore displayed an error message so that he can enter the correct number of values.

This is my code for the NEXT button event:

Private Sub Button40_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button40.Click

Dim newItem As String = TextBox1.Text
items.Add(newItem) 'Add new item to our list
'If Form2 exists then add the item to it's listbox
If frm1 IsNot Nothing AndAlso Not frm1.IsDisposed Then frm1.ListBox3.Items.Add(newItem)


' sender parameter is the control that triggered
' the Click event. Convert it to a Button:
Dim btn As Button = DirectCast(sender, Button)

' Now pass the button's Text property to the
' voice.Speak method:
Dim voice As New SpVoice
voice.Speak(btn.Text, SpeechVoiceSpeakFlags.SVSFlagsAsync)


TextBox1.Text = ""

If frm1 Is Nothing OrElse frm1.IsDisposed Then
frm1 = New ConfirmDestination
frm1.ListBox3.Items.AddRange(items.ToArray)
frm1.Show()
Else
frm1.Visible = True
End If

If TextBox1.Text.Length <= 5 Then
MsgBox("please enter 6 or 7 values in the textbox")


End Sub

TwoFaced
03-15-2007, 02:17 PM
You can't clear the textbox before you check it's length. Either check the length of the string you used to store the textbox's value or clear the textbox after the check has been made. It's this line that causes the problem. Textbox1.text = ""

Also the validation should be the first thing you do in that procedure. If the validation fails you should display the message the then use exit sub to return without executing the rest of the code.

MonaVohra
03-15-2007, 07:29 PM
thats true....thanks :)