Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Strings to ints

Strings to ints

Scheduled Pinned Locked Moved C#
question
5 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sroberts82
    wrote on last edited by
    #1

    I have a text box to get info off the user. It should be an int but i cant be sure so i must validate it first. How do I tell if a string is an int? I know i could do Convert.toInt.... and put it in a try catch but is there another way? Thanks in advance

    D S L T 4 Replies Last reply
    0
    • S sroberts82

      I have a text box to get info off the user. It should be an int but i cant be sure so i must validate it first. How do I tell if a string is an int? I know i could do Convert.toInt.... and put it in a try catch but is there another way? Thanks in advance

      D Offline
      D Offline
      Drew McGhie
      wrote on last edited by
      #2

      try int.TryParse()[^] Look up the overloads too, because it intially expects an int with no formatting whatsoever, but using system.globalization.numberstyle.* you can have it automaticlly handle commas, currency symbols, etc.

      1 Reply Last reply
      0
      • S sroberts82

        I have a text box to get info off the user. It should be an int but i cant be sure so i must validate it first. How do I tell if a string is an int? I know i could do Convert.toInt.... and put it in a try catch but is there another way? Thanks in advance

        S Offline
        S Offline
        Stefan Troschuetz
        wrote on last edited by
        #3

        When using Framework 2.0 the MaskedTextBox[^] class may be an option too.


        www.troschuetz.de

        1 Reply Last reply
        0
        • S sroberts82

          I have a text box to get info off the user. It should be an int but i cant be sure so i must validate it first. How do I tell if a string is an int? I know i could do Convert.toInt.... and put it in a try catch but is there another way? Thanks in advance

          L Offline
          L Offline
          ldaoust
          wrote on last edited by
          #4

          You could use a regular expression to test the string to see that all characters are numeric. Somthing like:private static Regex _isNumber = new Regex(@"^\d+$"); public static bool IsInteger(string theValue) { Match m = _isNumber.Match(theValue); return m.Success; }
          *not tested


          Louis * google is your friend * -- modified at 16:36 Wednesday 19th April, 2006

          1 Reply Last reply
          0
          • S sroberts82

            I have a text box to get info off the user. It should be an int but i cant be sure so i must validate it first. How do I tell if a string is an int? I know i could do Convert.toInt.... and put it in a try catch but is there another way? Thanks in advance

            T Offline
            T Offline
            The Nemesis
            wrote on last edited by
            #5

            You can also try to capture the KeyPress event on the text box and check the user input. void TextBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { if(!Char.IsDigit(e.KeyChar) && e.KeyChar != (char)8) { e.Handled = true; } else { e.Handled = false; } } (char) 8 is the backspace key...so the user can actually delete the entry as well !

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups