Thread: printing integer in Edit control

  1. #1
    Registered User
    Join Date
    Feb 2010

    printing integer in Edit control

    I wrote this program that has several Edit controls in the main window and numbers printed in them when you start the application. In most Edit controls I print doubles, but in one Edit control I print an integer. I used this piece of code that I found in the 'Programming Windows' book of Charles Petzold to convert one or more numbers in a wide-character string:

    TCHAR* NumberToString(TCHAR* szFormat, ...)
    	TCHAR szBuffer[1024];
    	va_list pArgList;
    	va_start(pArgList, szFormat);
    	_vsnwprintf_s(szBuffer, sizeof(szBuffer)/sizeof(TCHAR), sizeof(szBuffer)/sizeof(TCHAR), szFormat, pArgList);
    	szFormat = szBuffer;
    	return szFormat;
    and then I use these two lines in the WinProc to print the variable set.timesteps, which is actually 1, in an edit control with handle hwndEdit[9]:
    SetWindowText(hwndEdit[9], tempbuf);
    tempbuf = NumberToString(TEXT("%d"), set.timesteps);
    This works perfectly fine on several pcs except on one pc of a colleague of mine. There a 'V' is printed in the edit control. This seems like a Unicode problem but I have no clue as to what can be the cause.

  2. #2
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    If you just want to put an int in an edit box, use SetDlgItemInt. The NumberToString function as it stands is useless. For all intents and purposes szBuffer doesn't exist after the return, so the returned pointer points to what could very well be random garbage, which it seems in one case is exactly what you've ended up with.

    The options are to pass the output buffer as a second parameter so it will still exist after the function, or use malloc/_tcsdup to return a heaped copy of the string which you'll have to subsequently free.

    The original MessageBoxPrintf works fine because szBuffer is only used within the function.

  3. #3
    Registered User
    Join Date
    Feb 2010
    yeah, I guess you're right that the function will return a pointer that is not be valid anymore. I wonder if I changed something to Charles Petzold's function, because I'm sure he wouldn't write a bad function like that. I'll have to look that up in his book again though.
    Is it possible to use SetDlgItemInt for an Edit Control that is not in a Dialog Box? These Edit Controls are all shown in the main window.
    I don't really know how to make a heaped copy using malloc/_tcsdup, but I'm sure that I can look it up. Thanks for the reply.

  4. #4
    Registered User
    Join Date
    Feb 2010
    ok, I understand what I did now. The original function I changed was MessageBoxPrintf, that is why you mentioned that name. I figured that I didn't just want the function to print the string, but also return it so I could use it in different functions. Clearly, I didn't realize then that the strings can't be copied by just copying the pointers. To resolve the problem I only replaced the line

    szFormat = szBuffer;

    szFormat = _wcsdup( szBuffer );
    basically just re-using the pointer to the input string. I assume that is allowed.

  5. #5
    train spotter
    Join Date
    Aug 2001
    near a computer
    That example from Petzoid is for an unknown (at compile time) number of argumants formatted into a string. I do not think you have an unknown number of items to format into a string, and so I would not use that code.

    Edits can have the ES_NUMBER style which restricts input to digits only (no decimal point etc).

    I use _snprintf() to format char strings.
    This is a safe version of sprintf() [which takes the size of the char array, not including the NULL terminator) as the second argument.]

    char     szBuf[MAX_PATH]={0};
    int        iSomeNumber = 12345, iAnotherNumber = 234;
    _snprintf(szBuf, MAX_PATH-1, "The numbers are %d and %d", iSomeNumber, iAnotherNumber );
    //set the edits text
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  6. #6
    Registered User
    Join Date
    Feb 2010
    hi novavain, indeed, I don't need multiple arguments so I could definitely use a simpler function. I used the Petzold function a while ago, because I actually do need to turn both integers as well as floating point numbers into strings. The Petzold function did that so I guess sticked to it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  2. No Match For Operator+ ???????
    By Paul22000 in forum C++ Programming
    Replies: 24
    Last Post: 05-14-2008, 10:53 AM
  3. Edit control template
    By BobS0327 in forum Windows Programming
    Replies: 3
    Last Post: 04-08-2005, 12:10 PM
  4. Controlling an edit control
    By master5001 in forum Windows Programming
    Replies: 2
    Last Post: 10-16-2001, 03:08 PM
  5. Rich edit control example Win API
    By Echidna in forum Windows Programming
    Replies: 1
    Last Post: 09-17-2001, 02:12 AM