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. Managed C++/CLI
  4. TextBox: Conversion from String to Double

TextBox: Conversion from String to Double

Scheduled Pinned Locked Moved Managed C++/CLI
winformshelpquestion
12 Posts 3 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.
  • J J_E_D_I

    Hi, I am new to CLI-Windows Forms programming and I need to perform this simple task: 1) Allow the user to enter a value in a Text Box (textBox1) 2) Assign such value to a variable of type double (variable1) [I am having difficulties casting the string to double] 3) Perform computations on that variable [I should be able to do this once it's a double] 4) Display the resulting value in another Text Box (textBox2) Can someone be so kind to help me with the correct syntax I should use for each step? Many thanks.

    L Offline
    L Offline
    led mike
    wrote on last edited by
    #3

    J_E_D_I wrote:

    Hi, I am new to CLI-Windows Forms programming and I need to perform this simple task:

    See if this helps[^]

    led mike

    M 1 Reply Last reply
    0
    • L led mike

      J_E_D_I wrote:

      Hi, I am new to CLI-Windows Forms programming and I need to perform this simple task:

      See if this helps[^]

      led mike

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #4

      Awesome! :-D

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      L 1 Reply Last reply
      0
      • M Mark Salsbery

        For step 2: Double.Parse Method[^] Double.TryParse Method[^] For step 4: Double.ToString Method[^]

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        J Offline
        J Offline
        J_E_D_I
        wrote on last edited by
        #5

        Thanks for the advice, it works. But I obviously get a crash when I leave the text box empty or I enter a non numerical value. Here is the code I use to assign the value entered in the text box (as string) to a type double variable: // variable1 = double::Parse(textBox1->Text); // How can I solve this? Thanks!

        M 1 Reply Last reply
        0
        • J J_E_D_I

          Thanks for the advice, it works. But I obviously get a crash when I leave the text box empty or I enter a non numerical value. Here is the code I use to assign the value entered in the text box (as string) to a type double variable: // variable1 = double::Parse(textBox1->Text); // How can I solve this? Thanks!

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #6

          J_E_D_I wrote:

          But I obviously get a crash when I leave the text box empty or I enter a non numerical value.

          Use TryParse(). Mark

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          J 1 Reply Last reply
          0
          • M Mark Salsbery

            Awesome! :-D

            Mark Salsbery Microsoft MVP - Visual C++ :java:

            L Offline
            L Offline
            led mike
            wrote on last edited by
            #7

            Mark Salsbery wrote:

            Awesome!

            Nah, that was mildly amusing. This is awesome.[^]

            led mike

            M 1 Reply Last reply
            0
            • M Mark Salsbery

              J_E_D_I wrote:

              But I obviously get a crash when I leave the text box empty or I enter a non numerical value.

              Use TryParse(). Mark

              Mark Salsbery Microsoft MVP - Visual C++ :java:

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

              I have tried: // double number; variable1 = double::TryParse(textBox1->Text, number); // With the code above it's better and the program does not crash anymore. However the problem is not completely solved because when I leave the text box empty or enter a character different from a number, the result of variable1 is zero, which causes an error in the calculation that follows. How can I show an "Error" message when this has happened? Alternatively, is there any way that I could make the textBox accept only numerical values and display an error in the other cases?

              M 1 Reply Last reply
              0
              • J J_E_D_I

                I have tried: // double number; variable1 = double::TryParse(textBox1->Text, number); // With the code above it's better and the program does not crash anymore. However the problem is not completely solved because when I leave the text box empty or enter a character different from a number, the result of variable1 is zero, which causes an error in the calculation that follows. How can I show an "Error" message when this has happened? Alternatively, is there any way that I could make the textBox accept only numerical values and display an error in the other cases?

                M Offline
                M Offline
                Mark Salsbery
                wrote on last edited by
                #9

                TryParse returns a bool.

                double variable1;
                if (double::TryParse(textBox1->Text, variable1))
                {
                // parse succeeded - safe to use use variable1
                }
                else
                {
                // parse failed
                }

                Did your Visual Studio not come with MSDN library? :) Mark

                Mark Salsbery Microsoft MVP - Visual C++ :java:

                J 1 Reply Last reply
                0
                • L led mike

                  Mark Salsbery wrote:

                  Awesome!

                  Nah, that was mildly amusing. This is awesome.[^]

                  led mike

                  M Offline
                  M Offline
                  Mark Salsbery
                  wrote on last edited by
                  #10

                  Indeed! :cool:

                  Mark Salsbery Microsoft MVP - Visual C++ :java:

                  1 Reply Last reply
                  0
                  • M Mark Salsbery

                    TryParse returns a bool.

                    double variable1;
                    if (double::TryParse(textBox1->Text, variable1))
                    {
                    // parse succeeded - safe to use use variable1
                    }
                    else
                    {
                    // parse failed
                    }

                    Did your Visual Studio not come with MSDN library? :) Mark

                    Mark Salsbery Microsoft MVP - Visual C++ :java:

                    J Offline
                    J Offline
                    J_E_D_I
                    wrote on last edited by
                    #11

                    This is a fantastic answer! Thank you so much, it solved the problem.

                    Mark Salsbery wrote:

                    Did your Visual Studio not come with MSDN library?

                    Probably you guys super expert programmers manage to find what you are looking for in MSDN. I program in my spare time just for fun and I do not find them helpful AT ALL!! :sigh: They seriously lack of simple examples for beginners like me and they take too much for granted. Regarding TryParse there was no specific example for c++. Cheers!

                    M 1 Reply Last reply
                    0
                    • J J_E_D_I

                      This is a fantastic answer! Thank you so much, it solved the problem.

                      Mark Salsbery wrote:

                      Did your Visual Studio not come with MSDN library?

                      Probably you guys super expert programmers manage to find what you are looking for in MSDN. I program in my spare time just for fun and I do not find them helpful AT ALL!! :sigh: They seriously lack of simple examples for beginners like me and they take too much for granted. Regarding TryParse there was no specific example for c++. Cheers!

                      M Offline
                      M Offline
                      Mark Salsbery
                      wrote on last edited by
                      #12

                      J_E_D_I wrote:

                      there was no specific example for c++

                      Hint: Use the C# examples. For most examples of using .NET framework classes, the only difference will be '->' in c++ instead of '.' in c# gcnew in c++ instead of new in c#. Cheers! :beer: Mark

                      Mark Salsbery Microsoft MVP - Visual C++ :java:

                      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