Thread: Help with text box

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    1

    Help with text box

    Hello all,

    I new to Win32 programming with C++. I am trying to get a text box to accept integer or decimal numbers only. I was hoping there would some method like Tryparse to do this.
    I am not using any MFC or windows forms. Pure C++ only.
    I have the following code in place:
    Code:
    TR szText1 = new WCHAR[bufSize];
    LPWSTR szText2 = new WCHAR[bufSize];
    LPWSTR szText3 = new WCHAR[bufSize];
    double num1, num2, num3;
    GetWindowText(hwndThickness, szText1, bufSize);
    GetWindowText(hwndTheta, szText2, bufSize);
    GetWindowText(hwndTexheight, szText3, bufSize);
    num1=_wtof(szText1);
    num2=_wtof(szText2);
    num3=_wtof(szText3);
    if(_tcslen(szText1)==0 || _tcslen(szText2)==0 || _tcslen(szText3)==0)
    {
    MessageBox(hWnd, L"One or more input fields left blank", L"!!!!", MB_OK);
    }
    Thanks

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    if you're using C++, then you can use boost. boost includes a lexical_cast function that allows the conversion of any type to any other type, so long as they both support insertion and extraction from standard C++ streams.

    so you could have something like the following:
    Code:
    #include <boost/lexical_cast.hpp>
    
    const int DoubleConversionSuccess = 0;
    const int DoubleConversionError = -1;
    
    int GetDoubleFromString(const std::string& str, double& d)
    {
      try
      {
        d = boost::lexical_cast<double>(str);
        return DoubleConversionSuccess;
      }
      catch (boost::bad_lexical_cast& blc)
      {
        return DoubleConversionError;
      }
    }

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Here's an idea - repaste your code without using HTML, so your code is readable.

    It should look like this when you're done.
    Code:
    bool CheckEmpty(string file)
    {
      if (s.empty()) {
        return true;                //true if stack is empty when it shouldnt be
      } else {
        return false;               //false if stack is not empty when it should be
      }
    }
    There's a preview button - use it to make sure your post is good.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Elkvis View Post
    if you're using C++, then you can use boost. boost includes a lexical_cast function that allows the conversion of any type to any other type, so long as they both support insertion and extraction from standard C++ streams.

    so you could have something like the following:
    Code:
    #include <boost/lexical_cast.hpp>
    
    const int DoubleConversionSuccess = 0;
    const int DoubleConversionError = -1;
    
    int GetDoubleFromString(const std::string& str, double& d)
    {
      try
      {
        d = boost::lexical_cast<double>(str);
        return DoubleConversionSuccess;
      }
      catch (boost::bad_lexical_cast& blc)
      {
        return DoubleConversionError;
      }
    }
    Is there a point in circumventing the exception system? Catching an exception just to return some error is usually a sign of bad design.

    Btw, dg88, it isn't possible to create GUI applications with pure C++. You are, in fact, using Win32 in addition to C++. I'd recommend you get a GUI library such as Qt. It will be much easier.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Elysia View Post
    Is there a point in circumventing the exception system? Catching an exception just to return some error is usually a sign of bad design.
    I couldn't agree more, but since he's using pure win32 api, I assumed that he wouldn't want a bunch of stray try/catch blocks scattered throughout the code, and I doubt win32 gui code is exception-safe. if the system calls your WndProc function, and it throws an exception, where does it get caught?

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Then you put try/catch in your WinProc, and nowhere else.
    You can also wrap resources in RAII containers to make your Win32 code exception safe.
    There is usually no excuse for putting it in a utility function.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replace wildcard text in file with specific text.
    By untz in forum C++ Programming
    Replies: 4
    Last Post: 11-22-2010, 06:35 PM
  2. appending text to existing text file
    By kpax in forum C Programming
    Replies: 8
    Last Post: 05-28-2008, 08:46 AM
  3. Type text = Press button = Display text in Google?
    By Raze88 in forum C++ Programming
    Replies: 4
    Last Post: 03-20-2008, 08:39 AM
  4. Replies: 4
    Last Post: 01-03-2006, 03:02 AM
  5. create a text file with data using text editor
    By fried egg in forum C Programming
    Replies: 3
    Last Post: 03-14-2002, 09:11 PM