Making TextBox handle Numerica values?

Hi everybody,

I am trying to learn visual C++ and this is my first problem in building a Widows application:

A textbox control can handle data of Text type only. For example, if I have a form with a TextBox named textBox1, and I have the following code under a button:

int x;

x = textBox1->text;

I woul get the followin error when compiling:

error C2440: '=' : cannot convert from 'System::String ^' to 'int'

Now, how can I change the data type of a textbox so that it can take numbers

Would appreciate any input.

Thank you in advance,



Answer this question

Making TextBox handle Numerica values?

  • Bil Click

    Thanks a lot Oshah.

    This is very useful.


  • SauravSen

    Hello maman. Assuming your code is correct I will apply my knowledge of .NET to the problem.

    The problem is textBox1->Text is a string. This string can contain any type of value.

    there is a method that converts textr to integers. It is called Int32::Parse
    It is a static method of the class Int32.
    The following code would be

    // Int32::Parse(textBox1->Text); // don't use this unless you know for a fact it will never be an invalid value otherwise it will throw an exception.
    Int32 result;
    if (Int32::TryParse(textBox1->Text, result)==true)
    { MessageBox::Show(result.ToString()); }
    else
    {
    MessageBox::Show("Invalid number specified");
    }


  • Sergey Barinov

    Thanks a lot for your response, MakD.

    I tryied your code and it worked fine.

    woul you plesae help me figuring out the following

    If we want to display an output of numiric vallue through a taxtbox (or a label), do we have to convert it to string first if yes, how

    One more thing please, what if we are dealing with a float and not an integer

    Thank you once again.


  • edwin220

    To perform the reverse of converting a String^ to int (ie. convert an int to a string), use the ToString method of int.

    textBox1->Text = x.ToString();

    You can control the format of the output by using the second overload of ToString():

    textBox1->Text = x.ToString("00");// pads single digit values with an extra zero: eg. 01 02

    (the output format is similar to the format string MS Excel uses).



  • Making TextBox handle Numerica values?