• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 8191
  • Last Modified:

Making TextBox handle Numeric values? [Visual C++]

Hello,

Ok, here I am asking the same question again! I was thinking it is so basic an I would have no problem finding an answer in no time. But after searching and asking for days, I got nothing!

Here is the question:
 
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 following error when compiling:

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


The same applies to reading numeric values from a textbox.

Now, how can I change the data type of a textbox to integer or float so that it can handle numirec values?

Would appreciate any input.

Thank you in advance,

0
Mahmoodaman
Asked:
Mahmoodaman
  • 7
  • 7
1 Solution
 
mahesh1402Commented:
convert string to int using function like atoi()

int x;
x = atoi(textBox1->text);

to convert to float you may use atof()

float x;
x  = atof(textBox1->text);

-MAHESH
0
 
AlexFMCommented:
int x;

try
{
     x = Int32::Parse(textBox1->text);
}
catch (FormatException^ e)
{
   // handle error
}
catch (OverflowException ^ e)
{
   // handle error
}

Take a look also at MaskedTextBox class.
0
 
MahmoodamanAuthor Commented:
Thank you for your respones MAHESH,

I am getting this error:
  error C3861: 'atoi': identifier not found

Any idea what might be wrong?


0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
MahmoodamanAuthor Commented:
Thank you for your response AlexFM,

I am gettin the following error when trying your code:

error C2248: 'System::Windows::Forms::Control::text' : cannot access private member declared in class 'System::Windows::Forms::Control  

Any idea what might be wrong?      
0
 
AlexFMCommented:
Should be: textBox1->Text

I just copied your code, this was mistake.
0
 
AlexFMCommented:
About atoi - you need to add appropriate include line: <stdlib.h>
But it is not good idea to use CRT functions in C++/CLI application. .NET contains all required functions and replaces CRT for managed code.
0
 
MahmoodamanAuthor Commented:
Thank you AlexFM,

I really appreciate your help.

I added #include<stdlib.h> and changed "text" to "Text". I am now getting the following error:

error C2664: 'atoi' : cannot convert parameter 1 from 'System::String ^' to 'const char *'

Would you please suggest a reason for this error?
0
 
AlexFMCommented:
Please show your code. Where did you find char* in managed application? Do you use my suggestion with Int32::Parse or atoi function?
Again: you don't need atoi and stdlib.h, you need Int32::Parse.

General advice for everybody who starts working in C++/CLI: try C# first. There is no need to make Windows Forms application in C++/CLI. C# is much better for this purpose. C++/CLI should be used for interoperability, accessing unmanaged resources and legacy C++ code.
0
 
MahmoodamanAuthor Commented:
I am trying this on a form with just a textbox (named textBox1) and a button. Here is the code of the button:

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
      int x;
      x = Int32::Parse(textBox1->text);
            
       }

This produces the following error:
error C2248: 'System::Windows::Forms::Control::text' : cannot access private member declared in class 'System::Windows::Forms::Control'

As for C#, is it of the same processing speed as C++? Actually, I have written a program in VB (where life is much easier!). But I am having a problem with its speed. I decided to convert it into C++ as it is the fastes programming language ,as far as I know.

So, how is the speed of C# copmared to C++?

Many Thanks
0
 
MahmoodamanAuthor Commented:
Sorry!
it is my mistake. I wrote "text", instead of "Text" here (it is the VB effect!)

Now how to deal with a float? and how is the speed of C#?

Sorry once again. . .
0
 
AlexFMCommented:
Use Convert.ToSingle Method (String) method. It parses string and converts it to float value.
Convert class contains set of function for parsing all basic types.

float f;

try
{  
    f = Convert::ToSingle(textBox1->Text);
}
catch (FormatException^ e)
{
   // handle error
}
catch (OverflowException ^ e)
{
   // handle error
}

0
 
AlexFMCommented:
About C++/CLI vs C#.
.NET contains number of languages: C#, VB .NET, C++/CLI etc. C# is better choice for programmer with C++ experience. VB is a good choice for VB6 programmer, though it looks like VB6, this means, not like "real" programming language. Microsoft encourages developers to work in C#, and gives better IDE support for this language. Some advanced language features of C# are not available in VB. C++/CLI has the same strength as C#, but it's syntax is more complicated and less readable. Create C# Windows Forms application and C++/CLI application, and you can see that C# looks much better.
All .NET languages are translated to the same intermediate langaage: MSIL. Different compilers (C#, BV, C++) convert source code to the same language. Performance of all .NET languages is absolutely the same.

So, why we need C++/CLI at all? It's strength is in interoperability. This language allows to call unmanaged code directly, which is impossible in C#. This allows to use legacy C++ code, access low-level API, write real time applications with hardware access, make time critical operations in unmanaged code. Here is the strength of C++/CLI. I beleive that we don't need C++/CLI unless we must do some unmanaged tasks.
0
 
MahmoodamanAuthor Commented:
Thank you for your reply AlexFM,

"Performance of all .NET languages is absolutely the same."

Does it mean that the running speed of a program built in C++ is the same as one built in VB?

Regards,
0
 
AlexFMCommented:
Yes. To be presize: performance of programs written in C++/CLI, VB .NET and C# is absolutely the same.
I am not talking about: unmanaged C++, VB6 - these are different languages.
0
 
MahmoodamanAuthor Commented:

I find this really strange! If I knew this, I would have gone for VB.NET inctead of C++!

I will look into this.

Thanks a lot AlexFM
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 7
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now