CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2012
    Posts
    13

    Convert to float from textbox

    Hallo i try to convert the value from the textbox to float i use this code :
    float a;
    if(rb1->Checked){a=Convert::ToFloat(tb1->Text);tb26->Text=Convert::ToString(a);}

    what have i done wrong ?

  2. #2
    Join Date
    Jan 2009
    Posts
    596

    Re: Convert to float from textbox

    Quote Originally Posted by Pavaka View Post
    Hallo i try to convert the value from the textbox to float i use this code :
    Code:
    float a;
    if(rb1->Checked){a=Convert::ToFloat(tb1->Text);tb26->Text=Convert::ToString(a);}
    what have i done wrong ?
    Well, putting all that code on one line is not a good idea for starters
    But to answer your question properly, you need to give us more details as to what problem you are having.

  3. #3
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Convert to float from textbox

    Actually, there is no such function as Convert::ToFloat(). The "official" .NET name of that type is Single, and float is just an alias name for that type in C++/CLI (and, AFAIK, in C#). Consequentially, the function you need to call is Convert::ToSingle().

    Also, as already has been pointed out, putting all that code on a single line without a single whitespace is no good idea with regard to coding style, though of course it doesn't keep the code from compiling successfully.

    Finally, this is a C++/CLI question, and as such it's in the wrong place here. There's a separate forum section for C++/CLI: http://www.codeguru.com/forum/forumdisplay.php?f=17
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  4. #4
    Join Date
    Mar 2004
    Location
    Central Florida
    Posts
    293

    Re: Convert to float from textbox

    You could simply use atof()
    "Effective teaching is the essence of leadership..."

    "There is no substitute for a carefully thought-out design."

    If you have found this post to be useful, please Rate it.

  5. #5
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: Convert to float from textbox

    Quote Originally Posted by mlgoff View Post
    You could simply use atof()
    Not quite simple.
    You can use atof or any other CRT function in managed C++/CLI, but you have to "scratch your had with the foot".
    Something like this:
    Code:
    #include <stdlib.h>
       // ...
       String^ str = textBox1->Text;
       char* szValue = 
          (char*)(void*)System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(str);
       float fValue = (float)atof(szValue);
    Much easier and correct is to use the "managed way", like Eri already suggested:
    Code:
       float fValue = Convert::ToSingle(textBox1->Text);
    So...
    When in Rome, do as the Romans do!
    In other words, when in managed C++/CLI, do as .NET programmers do.

    @Pavaka:
    I'm just curious...
    Is your IntelliSense tool out of order and is showing Convert::ToFloat method?
    Also, have you lost the Internet connection with MSDN Library?


    [ Moved thread ]
    Last edited by ovidiucucu; March 17th, 2012 at 04:43 AM.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured