Results 1 to 4 of 4

Thread: text box String Loop Issue

  1. #1

    Thread Starter
    Member
    Join Date
    May 2011
    Posts
    42

    Smile 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.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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?

  3. #3

    Thread Starter
    Member
    Join Date
    May 2011
    Posts
    42

    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

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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:
    1. Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    2.  
    3.         If TextBox1.Text.Length > 3 Then
    4.             MessageBox.Show("Name can only be a maximum of 3 characters.", "Too Long", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    5.             Exit Sub
    6.         End If
    7.  
    8.         If (e.KeyChar >= Chr(32) And e.KeyChar <= Chr(63)) Or (e.KeyChar >= Chr(91) And e.KeyChar <= Chr(94)) _
    9.             Or (e.KeyChar = Chr(96)) Or (e.KeyChar >= Chr(123) And e.KeyChar <= Chr(126)) Then
    10.             e.Handled = True
    11.         End If
    12.  
    13.     End Sub

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