Results 1 to 13 of 13

Thread: [RESOLVED] [2005] String question..

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2007
    Location
    NYC
    Posts
    86

    Resolved [RESOLVED] [2005] String question..

    Hello,
    I'm making a program in which I want only integers to be entered into the textbox. I tried making a messagebox appear if the text entered into the textbox is not an integer, but I can't seem to get it right. How do you make an alert telling the user to enter an integer if they enter a string, or decimal, etc., etc. into the textbox?

    Thanks!

    Regards,
    Silencer
    There is a thin line between hobby, and obsession.

    If you found my post helpful, please rate it.
    My Laptop System Stats: nVidia 8800 graphics card, Two Intel Core Duo processors, 3.5 GB RAM, Windows Vista Ultimate, 15.4 Widescreen Brightview display, Built-in Webcam and Microphone.
    My Alienware System Stats: nVidia 8800 graphics card, Intel Core Duo Extreme Edition processors, 4 gb RAM, Windows Vista Ultimate, 21 Inch Plasma Display, 42 Inch LCD HD display.

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Apr 2007
    Location
    NYC
    Posts
    86

    Re: [2005] String question..

    I also want to make an error messagebox show up when there is no text in textbox1, and not get a non-responsive program. Here is my code:
    vb Code:
    1. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    2.         Dim agerange As Integer
    3.  
    4.  
    5.         agerange = TextBox1.Text
    6.  
    7.  
    8.             Select Case agerange
    9.  
    10.                 Case 1 To 3
    11.                     MessageBox.Show("You are a Toddler", "You are a Toddler", MessageBoxButtons.OK, MessageBoxIcon.Information)
    12.                 Case 4 To 10
    13.                     MessageBox.Show("You are a Youngster", "You are a Youngster", MessageBoxButtons.OK, MessageBoxIcon.Information)
    14.                 Case 11 To 12
    15.                     MessageBox.Show("You are a Pre-teen", "You are a Pre-teen", MessageBoxButtons.OK, MessageBoxIcon.Information)
    16.                 Case 13 To 19
    17.                     MessageBox.Show("You are a Teenager", "You are a Teenager", MessageBoxButtons.OK, MessageBoxIcon.Information)
    18.                 Case 20 To 29
    19.                     MessageBox.Show("You are in your twenties", "You are in your twenties", MessageBoxButtons.OK, MessageBoxIcon.Information)
    20.                 Case 30 To 39
    21.                     MessageBox.Show("You are in your thirties", "You are in your thirties", MessageBoxButtons.OK, MessageBoxIcon.Information)
    22.                 Case 40 To 49
    23.                     MessageBox.Show("You are in your forties", "You are in your forties", MessageBoxButtons.OK, MessageBoxIcon.Information)
    24.                 Case 50 To 59
    25.                     MessageBox.Show("You are in your fifties", "You are in your fifties", MessageBoxButtons.OK, MessageBoxIcon.Information)
    26.                 Case 60 To 69
    27.                     MessageBox.Show("You are in your sixties", "You are in your sixties", MessageBoxButtons.OK, MessageBoxIcon.Information)
    28.  
    29.         End Select
    30.             Me.Refresh()
    31.             TextBox1.Text = ""
    32.  
    33.     End Sub
    34.  
    35.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    36.         Me.AcceptButton = Button2
    37.         Me.Button2.Text = "&How Old Are You?"
    38.         Me.Button1.Text = "&Exit"
    39.     End Sub
    40. End Class
    There is a thin line between hobby, and obsession.

    If you found my post helpful, please rate it.
    My Laptop System Stats: nVidia 8800 graphics card, Two Intel Core Duo processors, 3.5 GB RAM, Windows Vista Ultimate, 15.4 Widescreen Brightview display, Built-in Webcam and Microphone.
    My Alienware System Stats: nVidia 8800 graphics card, Intel Core Duo Extreme Edition processors, 4 gb RAM, Windows Vista Ultimate, 21 Inch Plasma Display, 42 Inch LCD HD display.

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

    Re: [2005] String question..

    There are numerous threads dedicated to preventing non-numeric input in a TextBox. Of course, the most logical way to get numerical input from the user is with a NumericUpDown control. If you are going to use a TextBox and you want to reject invalid input as it's typed then I suggest you consult those existing threads. If you prefer to validate after the fact then the Integer.TryParse method is for you.
    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

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Apr 2007
    Location
    NYC
    Posts
    86

    Re: [2005] String question..

    Thanks, how would I put the Integer.tryparse into my code above, and also make an error messagebox show up when there is no text in textbox1, and not get a non-responsive program.
    There is a thin line between hobby, and obsession.

    If you found my post helpful, please rate it.
    My Laptop System Stats: nVidia 8800 graphics card, Two Intel Core Duo processors, 3.5 GB RAM, Windows Vista Ultimate, 15.4 Widescreen Brightview display, Built-in Webcam and Microphone.
    My Alienware System Stats: nVidia 8800 graphics card, Intel Core Duo Extreme Edition processors, 4 gb RAM, Windows Vista Ultimate, 21 Inch Plasma Display, 42 Inch LCD HD display.

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

    Re: [2005] String question..

    What does the Integer.TryParse method do? Have you checked? If not then I suggest that you do because you can probably work it out for yourself. That would be preferable, would it not? My philosophy is "try first, then ask if you fail". I tend to offer guidance rather than outright solutions in many cases in the hope that others will do the same.
    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

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Apr 2007
    Location
    NYC
    Posts
    86

    Re: [2005] String question..

    It converts the string representation of a number in a specified style and culture-specific format to its 32-bit signed integer equivalent. A return value indicates whether the operation succeeded.


    Dim agerange As Integer
    Dim s As String
    Dim style As Globalization.NumberStyles
    Dim provider As IFormatProvider
    Dim result As Integer
    Dim returnValue As Boolean

    returnValue = Integer.TryParse(s, style, provider, result)

    thats how you would use it, but I don't know what to assign provider and s...I was thinking s is textbox1.text, but not sure.
    There is a thin line between hobby, and obsession.

    If you found my post helpful, please rate it.
    My Laptop System Stats: nVidia 8800 graphics card, Two Intel Core Duo processors, 3.5 GB RAM, Windows Vista Ultimate, 15.4 Widescreen Brightview display, Built-in Webcam and Microphone.
    My Alienware System Stats: nVidia 8800 graphics card, Intel Core Duo Extreme Edition processors, 4 gb RAM, Windows Vista Ultimate, 21 Inch Plasma Display, 42 Inch LCD HD display.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Apr 2007
    Location
    NYC
    Posts
    86

    Re: [2005] String question..

    I tried using your code which you posted in another post, and it worked, but after I hit ok in the messagebox that said the value you have entered is not a valid number, it gave me a nonresponsive program...
    vb Code:
    1. Dim number As Double
    2.         If Double.TryParse(TextBox1.Text, number) Then
    3.             'The text is a valid number.
    4.             TextBox1.Text = number.ToString("n2") 'Format as a number with 2 decimal places.
    5.         Else
    6.             MessageBox.Show("The value entered is not a valid number.")
    7.             TextBox1.Clear()
    8.         End If
    There is a thin line between hobby, and obsession.

    If you found my post helpful, please rate it.
    My Laptop System Stats: nVidia 8800 graphics card, Two Intel Core Duo processors, 3.5 GB RAM, Windows Vista Ultimate, 15.4 Widescreen Brightview display, Built-in Webcam and Microphone.
    My Alienware System Stats: nVidia 8800 graphics card, Intel Core Duo Extreme Edition processors, 4 gb RAM, Windows Vista Ultimate, 21 Inch Plasma Display, 42 Inch LCD HD display.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Apr 2007
    Location
    NYC
    Posts
    86

    Re: [2005] String question..

    So far this is my code, with your code ammended at the top, I disregarded the Integer.tryparse method, it seemed unfit for the problem. This method you had, works, but I still get an error, with the piece of text that says
    "agerange = Textbox1.text"
    vb Code:
    1. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    2.         Dim number As Double
    3.         If Double.TryParse(TextBox1.Text, number) Then
    4.             'The text is a valid number.
    5.             TextBox1.Text = number.ToString("n2") 'Format as a number with 2 decimal places.
    6.         Else
    7.             MessageBox.Show("The value entered is not a valid number.")
    8.             TextBox1.Clear()
    9.         End If
    10.         Dim agerange As Integer
    11.  
    12.  
    13.         agerange = TextBox1.Text
    14.  
    15.  
    16.         Select Case agerange
    17.  
    18.             Case 1 To 3
    19.                 MessageBox.Show("You are a Toddler", "You are a Toddler", MessageBoxButtons.OK, MessageBoxIcon.Information)
    20.             Case 4 To 10
    21.                 MessageBox.Show("You are a Youngster", "You are a Youngster", MessageBoxButtons.OK, MessageBoxIcon.Information)
    22.             Case 11 To 12
    23.                 MessageBox.Show("You are a Pre-teen", "You are a Pre-teen", MessageBoxButtons.OK, MessageBoxIcon.Information)
    24.             Case 13 To 19
    25.                 MessageBox.Show("You are a Teenager", "You are a Teenager", MessageBoxButtons.OK, MessageBoxIcon.Information)
    26.             Case 20 To 29
    27.                 MessageBox.Show("You are in your twenties", "You are in your twenties", MessageBoxButtons.OK, MessageBoxIcon.Information)
    28.             Case 30 To 39
    29.                 MessageBox.Show("You are in your thirties", "You are in your thirties", MessageBoxButtons.OK, MessageBoxIcon.Information)
    30.             Case 40 To 49
    31.                 MessageBox.Show("You are in your forties", "You are in your forties", MessageBoxButtons.OK, MessageBoxIcon.Information)
    32.             Case 50 To 59
    33.                 MessageBox.Show("You are in your fifties", "You are in your fifties", MessageBoxButtons.OK, MessageBoxIcon.Information)
    34.             Case 60 To 69
    35.                 MessageBox.Show("You are in your sixties", "You are in your sixties", MessageBoxButtons.OK, MessageBoxIcon.Information)
    36.  
    37.  
    38.  
    39.         End Select
    40.         Me.Refresh()
    41.         TextBox1.Text = ""
    42.  
    43.     End Sub
    44.  
    45.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    46.         Me.AcceptButton = Button2
    47.         Me.Button2.Text = "&How Old Are You?"
    48.         Me.Button1.Text = "&Exit"
    49.     End Sub
    There is a thin line between hobby, and obsession.

    If you found my post helpful, please rate it.
    My Laptop System Stats: nVidia 8800 graphics card, Two Intel Core Duo processors, 3.5 GB RAM, Windows Vista Ultimate, 15.4 Widescreen Brightview display, Built-in Webcam and Microphone.
    My Alienware System Stats: nVidia 8800 graphics card, Intel Core Duo Extreme Edition processors, 4 gb RAM, Windows Vista Ultimate, 21 Inch Plasma Display, 42 Inch LCD HD display.

  9. #9
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: [2005] String question..

    For getting the integer input you could use a UpDownNumeric Control,which will control the input itself
    Please mark you thread resolved using the Thread Tools as shown

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

    Re: [2005] String question..

    Double.TryParse attempts to parse a string representation of a double. Integer.TryParse attempts to parse a string representation of an integer. If you want to ensure that the user enters an integer then...
    vb Code:
    1. Dim myInteger As Integer
    2.  
    3. If Integer.TryParse(myString, myInteger) Then
    4.     'The operation was successful and the result is now contained in myInteger.
    5. Else
    6.     'The operation not successful.
    7. End If
    I'm with danasegarane though, as you'll note from my first reply.
    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

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Apr 2007
    Location
    NYC
    Posts
    86

    Re: [2005] String question..

    Got it, thanks a ton jmcilhinney, and danasegarane. I removed agerange, and changed everything to myInteger, and made it look like this:
    myInteger = Val(TextBox1.Text), the Val took away the error message when a string was imputted, and the tryparse gave an alert when it textbox1.text wasn't an integer.
    There is a thin line between hobby, and obsession.

    If you found my post helpful, please rate it.
    My Laptop System Stats: nVidia 8800 graphics card, Two Intel Core Duo processors, 3.5 GB RAM, Windows Vista Ultimate, 15.4 Widescreen Brightview display, Built-in Webcam and Microphone.
    My Alienware System Stats: nVidia 8800 graphics card, Intel Core Duo Extreme Edition processors, 4 gb RAM, Windows Vista Ultimate, 21 Inch Plasma Display, 42 Inch LCD HD display.

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

    Re: [2005] String question..

    You don't use Val() at all. The whole point of TryParse is that it trys to parse the string to an integer and tells you whether it succeeded or not. Read my previous post again, particularly the comments. If the operation is successful then the Integer variable contains the parsed value.
    Last edited by jmcilhinney; Apr 13th, 2007 at 11:30 PM.
    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

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Apr 2007
    Location
    NYC
    Posts
    86

    Re: [2005] String question..

    Got it..thanks.
    There is a thin line between hobby, and obsession.

    If you found my post helpful, please rate it.
    My Laptop System Stats: nVidia 8800 graphics card, Two Intel Core Duo processors, 3.5 GB RAM, Windows Vista Ultimate, 15.4 Widescreen Brightview display, Built-in Webcam and Microphone.
    My Alienware System Stats: nVidia 8800 graphics card, Intel Core Duo Extreme Edition processors, 4 gb RAM, Windows Vista Ultimate, 21 Inch Plasma Display, 42 Inch LCD HD display.

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