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. I need to convert a the string of a Text box to a Value

I need to convert a the string of a Text box to a Value

Scheduled Pinned Locked Moved C#
questionhelp
15 Posts 5 Posters 19 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.
  • G glennPattonWork3

    Hi All, Stupid Question time. If a value is read to a textbox it is a String, to convert it to a value in the past I have done:

    int Value = 0;
    Value = Convert.ToInt16(textBox1.Text)

    I'm sure of it and then done maths and operations on the Value such as:

    if(Value > 1000)
    {
    MessageBox.Show("Here!");
    }

    But it won't run in VS2022 it returns a System.Format.Exception error. What have I done? Glenn

    T Offline
    T Offline
    Tony Hill
    wrote on last edited by
    #2

    Don't use Convert as any error in the format of the input string will cause an exception. Instead use int.Parse because you can check the result of the parse operation to see if you have entered a valid or invalid string. From memory you should do the following

    if (int.Parse(textBox1.Text, out int result))
    {
    // if parsed ok then the result will be filled with the converted value
    }
    else
    {
    // display your error message here
    }

    Richard DeemingR 1 Reply Last reply
    0
    • G glennPattonWork3

      Hi All, Stupid Question time. If a value is read to a textbox it is a String, to convert it to a value in the past I have done:

      int Value = 0;
      Value = Convert.ToInt16(textBox1.Text)

      I'm sure of it and then done maths and operations on the Value such as:

      if(Value > 1000)
      {
      MessageBox.Show("Here!");
      }

      But it won't run in VS2022 it returns a System.Format.Exception error. What have I done? Glenn

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #3

      What value is in the textbox? If it can't be parsed as a short using the current culture settings, then Convert.ToInt16 will throw an exception. You should use TryParse[^] rather than the Convert method to avoid the "can't parse this" exception:

      if (int.TryParse(textBox1.Text, out int value))
      {
      if (value > 1000)
      {
      MessageBox.Show("Here!");
      }
      }


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      G 1 Reply Last reply
      0
      • T Tony Hill

        Don't use Convert as any error in the format of the input string will cause an exception. Instead use int.Parse because you can check the result of the parse operation to see if you have entered a valid or invalid string. From memory you should do the following

        if (int.Parse(textBox1.Text, out int result))
        {
        // if parsed ok then the result will be filled with the converted value
        }
        else
        {
        // display your error message here
        }

        Richard DeemingR Offline
        Richard DeemingR Offline
        Richard Deeming
        wrote on last edited by
        #4

        You mean TryParse; Parse returns the parsed value or throws an exception. :)


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

        T 1 Reply Last reply
        0
        • Richard DeemingR Richard Deeming

          You mean TryParse; Parse returns the parsed value or throws an exception. :)


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          T Offline
          T Offline
          Tony Hill
          wrote on last edited by
          #5

          I checked that twice and still missed that (Doh)

          1 Reply Last reply
          0
          • Richard DeemingR Richard Deeming

            What value is in the textbox? If it can't be parsed as a short using the current culture settings, then Convert.ToInt16 will throw an exception. You should use TryParse[^] rather than the Convert method to avoid the "can't parse this" exception:

            if (int.TryParse(textBox1.Text, out int value))
            {
            if (value > 1000)
            {
            MessageBox.Show("Here!");
            }
            }


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            G Offline
            G Offline
            glennPattonWork3
            wrote on last edited by
            #6

            Hmmm, I'm reading back a sensor, it's giving values all over the show!

            private void button1_Click(object sender, EventArgs e)
            {
            //int value = 0;

            // MessageBox.Show(textBox1.Text);

            if (int.TryParse(textBox1.Text, out int value))
            {
               
                if (value > 1000)
                {
                    MessageBox.Show("Here!");
                }
                MessageBox.Show(value.ToString());
            }
            
            MessageBox.Show(value.ToString());
            

            }

            value is taken as 0, despite textBox1.Text having 1234.56...

            L J 2 Replies Last reply
            0
            • G glennPattonWork3

              Hmmm, I'm reading back a sensor, it's giving values all over the show!

              private void button1_Click(object sender, EventArgs e)
              {
              //int value = 0;

              // MessageBox.Show(textBox1.Text);

              if (int.TryParse(textBox1.Text, out int value))
              {
                 
                  if (value > 1000)
                  {
                      MessageBox.Show("Here!");
                  }
                  MessageBox.Show(value.ToString());
              }
              
              MessageBox.Show(value.ToString());
              

              }

              value is taken as 0, despite textBox1.Text having 1234.56...

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #7

              glennPattonWork3 wrote:

              despite textBox1.Text having 1234.56...

              But 1234.56 is a Float/Double not an Int; the two are quite different.

              G 1 Reply Last reply
              0
              • G glennPattonWork3

                Hmmm, I'm reading back a sensor, it's giving values all over the show!

                private void button1_Click(object sender, EventArgs e)
                {
                //int value = 0;

                // MessageBox.Show(textBox1.Text);

                if (int.TryParse(textBox1.Text, out int value))
                {
                   
                    if (value > 1000)
                    {
                        MessageBox.Show("Here!");
                    }
                    MessageBox.Show(value.ToString());
                }
                
                MessageBox.Show(value.ToString());
                

                }

                value is taken as 0, despite textBox1.Text having 1234.56...

                J Offline
                J Offline
                jeron1
                wrote on last edited by
                #8

                Maybe try decimal.TryParse()? as the input is not an int.

                "the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle

                G 1 Reply Last reply
                0
                • L Lost User

                  glennPattonWork3 wrote:

                  despite textBox1.Text having 1234.56...

                  But 1234.56 is a Float/Double not an Int; the two are quite different.

                  G Offline
                  G Offline
                  glennPattonWork3
                  wrote on last edited by
                  #9

                  You are right, but even if make 1234 it still returns a 0...

                  L J 2 Replies Last reply
                  0
                  • G glennPattonWork3

                    You are right, but even if make 1234 it still returns a 0...

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #10

                    Come on Glen, you need to show us the exact code, and the exact text that you are using.

                    G 1 Reply Last reply
                    0
                    • J jeron1

                      Maybe try decimal.TryParse()? as the input is not an int.

                      "the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle

                      G Offline
                      G Offline
                      glennPattonWork3
                      wrote on last edited by
                      #11

                      Thanks, that appears to work! :-D

                      J 1 Reply Last reply
                      0
                      • G glennPattonWork3

                        You are right, but even if make 1234 it still returns a 0...

                        J Offline
                        J Offline
                        jeron1
                        wrote on last edited by
                        #12

                        Did the TryParse call succeed, return true?

                        "the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle

                        G 1 Reply Last reply
                        0
                        • L Lost User

                          Come on Glen, you need to show us the exact code, and the exact text that you are using.

                          G Offline
                          G Offline
                          glennPattonWork3
                          wrote on last edited by
                          #13

                          Hi, This is the code I have been trying to get all afternoon!

                          private void button1_Click(object sender, EventArgs e)
                          {
                          float ForceVal = 0;
                          //MessageBox.Show(weightTxt.Text);
                          //ForceVal = Convert.ToDecimal(weightTxt.Text);

                          	//int.TryParse(weightTxt.Text, out int value);
                          	decimal.TryParse(weightTxt.Text, out decimal value);
                             //  MessageBox.Show(value.ToString());
                               if (value > 1000)
                               {
                                   MessageBox.Show("Here!");
                               }
                               MessageBox.Show(value.ToString());
                           
                          	          
                          }
                          

                          I think that should do it. I'm hacking together a test program that interfaces to a Phidget load cell in trying to measure a value that needs to converted to Newtons from Grams (I think!). I hate the fact I wasted so much time trying to get the value to be used out of a darned label. I haven't really done much Windows code (I'm an embedded guy really) since VS2008 was the new kid on the block and times like these it shows! :sigh:

                          1 Reply Last reply
                          0
                          • J jeron1

                            Did the TryParse call succeed, return true?

                            "the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle

                            G Offline
                            G Offline
                            glennPattonWork3
                            wrote on last edited by
                            #14

                            Sort of I had to:

                            decimal.TryParse(weightTxt.Text, out decimal value);

                            as I was messing with a floating point number, haven't coded in Windows for a while, man it shows!

                            1 Reply Last reply
                            0
                            • G glennPattonWork3

                              Thanks, that appears to work! :-D

                              J Offline
                              J Offline
                              jeron1
                              wrote on last edited by
                              #15

                              :thumbsup: Good news!

                              "the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle

                              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