text box String Loop Issue
Hey peeps
im having an issue, im new to visual basic and dont know very much so im attempting a problem solving exercises from college to learn more.
i need it so my form 1 : text box the user enters a name and if they dont enter a name or if they enter wrong characters it loops and keeps asking them, if they enter a proper name it launches form2
this is my code i have made for it so far but its not working right. can someone help me out ?
Where i have " String " that dosnt work i need something in there i think, so that if 3 + any string characters it accepts as a name if that makes sence..
Public Class Form1
Dim Scores As Integer
Dim username As String
Private Sub BtnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnStart.Click
username = txtname.Text
Do Until txtname.Text = " String "
If txtname.Text = "" Then MsgBox(" you must enter your name")
If IsNumeric(txtname.Text) Then MsgBox(" please enter letters")
Exit Do
Loop
Me.Hide()
Form2.Show()
End Sub
Thanks for your help.
Re: text box String Loop Issue
The only way your code would work would be is someone placed the cursor in the textbox, hit the space bar then type in the word String
That way, the textbox would actually contain: " String" - which is all you are testing for.
What is a valid username? Against what are you validating the entries?
Re: text box String Loop Issue
Hey thanks for your reply
my username can be any name, i need it so what ever user enters there name it will eventualy be displayed later on in the program, so aslong as the text box allows any name thats 2 or 3 String + is what im trying to get it to do, and if if they enter numbers or $%£$"%&^ then msg box and loop until string is entered, sorry if i cant explain it well, im new it it all :o
Re: text box String Loop Issue
No need to loop...use the textbox itself. You will probably need to tinker with this a bit but you get the idea.
vb.net Code:
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If TextBox1.Text.Length > 3 Then
MessageBox.Show("Name can only be a maximum of 3 characters.", "Too Long", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Exit Sub
End If
If (e.KeyChar >= Chr(32) And e.KeyChar <= Chr(63)) Or (e.KeyChar >= Chr(91) And e.KeyChar <= Chr(94)) _
Or (e.KeyChar = Chr(96)) Or (e.KeyChar >= Chr(123) And e.KeyChar <= Chr(126)) Then
e.Handled = True
End If
End Sub