Results 1 to 9 of 9

Thread: [RESOLVED] Integer

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2009
    Posts
    8

    Resolved [RESOLVED] Integer

    I want a msg box to display when the values in a textbox are between 1 and 17.

    Should I use Integers and if statements?
    And if so how?

    Thanks

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Integer

    Yes, you should. Use Convert.ToInt32 or Int32.TryParse() to see if it's a number, then compare it to 1 and 17 (If something > 1 for example).

    Oh, and hello. Welcome to VBF.

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2009
    Posts
    8

    Re: Integer

    Thank you for your quick reply.

    However I am new to VB and need some more instruction.

    This is what I have so far.
    Code:
    Dim Soft17 As Integer
    Soft17 = 1, 17
    
    If TextBox6.Text = Soft17 Then
                MsgBox("Soft 17")
            End If

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

    Re: Integer

    You can't assigned a variable two values.

    Soft17 can equal 1 or 17 but not both at the same time. Try something like
    vb.net Code:
    1. Dim Soft17 As Integer = 0
    2.  
    3. 'some where else
    4. If Integer.Parse(TextBox6.Text) >= 1 And Integer.Parse(TextBox6.Text) <= 17 Then
    5.    MessageBox.Show("Whoop-dee-do!")
    6. Else
    7.    MessageBox.Show("Sorry...please try again.")
    8. End If
    Is that what you are after?

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    Re: Integer

    That code is only safe if the text is always an integer. If you can't be certain that the item in the textbox is always an integer, then you will need to use Integer.TryParse, which is a bit more convoluted, but won't throw exceptions.
    My usual boring signature: Nothing

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Integer

    Every time I see user input and some numeric data type, I cringe when i see something other than .TryParse. I don't understand why someone would answer any other way.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  7. #7
    Junior Member
    Join Date
    Mar 2009
    Posts
    21

    Re: Integer

    so would he have to rewwtir that as
    dim soft17 as integer.tryparse??

    if not where would i have to input that '.tryparse' linee plz?

  8. #8
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Integer

    vb Code:
    1. Dim Soft17 As Integer
    2. If Integer.TryParse(TextBox6.Text, Soft17) Then
    3.     If Soft17 >= 1 And Soft17 <= 17 Then
    4.         MsgBox("between 1 and 17")
    5.     Else
    6.         MsgBox("out of range number")
    7.     End If
    8. Else
    9.     MsgBox("Not a number")
    10. End If
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    Re: Integer

    The post I had refered too was deleted, rendering my comment a random critique of Mendhak.
    My usual boring signature: Nothing

Tags for this Thread

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