-
Using GetWindowText
I'm trying to get a number from an edit box and just have it multiplyed but since GetWindowText uses a character buffer i can't multiply it and if i use something like atoi() when i go to pass it back to the SetWindowText it tells me that it can't convert char to int. So how would i get an intiger from an edit box(i'm not using a dialog) do some mathmatical equation on it and return it to the SetWindoText to display it back in the edit box again?
-
Here is a sample code.
-----
myEditBox.SetWindowText("2002");
// Now you want to multiply 2002 by 2
CString num;
myEditBox.GetWindowText(&num);
long temp = atol(&num) * 2;
num.Format("%d", temp);
myEditBox.SetWindowText(num);
-----
Kuphryn
-
Still Doesn't Work
I'm still getting the error Cannot convert 'int' to 'const char *' that was my original problem. I'm usinc borlands compliler 5.5 if that makes any difference.
-
Oh. You're using Borland. I am not sure about Borland C++.
Kuphryn
-
anyone else
anyone else got any ideas how to make this work?
-
Code:
#include <windows.h>
#include <stdlib.h>
LRESULT CALLBACK WindowProc(HWND, UINT, WPARAM, LPARAM);
char szClassName[] = "WindowsApp";
HINSTANCE g_hInst;
int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nFunsterStil)
{
HWND hwnd;
MSG messages;
WNDCLASSEX wincl;
g_hInst = hThisInstance;
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProc;
wincl.style = CS_DBLCLKS;
wincl.cbSize = sizeof(WNDCLASSEX);
wincl.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor(NULL, IDC_ARROW);
wincl.lpszMenuName = NULL;
wincl.cbClsExtra = 0;
wincl.cbWndExtra = 0;
wincl.hbrBackground = (HBRUSH) GetStockObject(LTGRAY_BRUSH);
if(!RegisterClassEx(&wincl)) return 0;
hwnd = CreateWindowEx(0,szClassName, "Poor Man's Calc",
WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,
200,120, HWND_DESKTOP,NULL,
hThisInstance,NULL );
ShowWindow(hwnd, nFunsterStil);
UpdateWindow(hwnd);
while(GetMessage(&messages, NULL, 0, 0))
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
return messages.wParam;
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
static HWND hEdit,hButton;
char str[10];
int x;
switch (message)
{
case WM_CREATE:
hEdit = CreateWindow("EDIT","",WS_CHILD | WS_VISIBLE,
10,10,100,30,hwnd,NULL,g_hInst,NULL);
hButton = CreateWindow("BUTTON","Multiply by 10",
WS_CHILD | WS_VISIBLE,10,50,100,30,hwnd,NULL,g_hInst,NULL);
break;
case WM_COMMAND:
if((HWND) lParam == hButton){
GetWindowText(hEdit,str,10);
if(str[0] == 0)
break;
x = atoi(str);
if(x)
SetWindowText(hEdit,itoa(x*10,str,10));
else
MessageBox(hwnd,"Error",NULL,NULL);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}
Use atoi and itoa....
-
You could use GetDlgItemInt()
-
Thanks
-
i have one more probles, it works fine, you enter the number and it gets multiplyed but when i certain numbers though it returns a value thats not it multiplies like i could enter 6 and it will return the letter c. any one have a fix for this, also it doesn't read a number like 1.2 it only multiplys the 1 in 1.2
And is there a style or something that only allows numbers to be enter in an edit box?
-
use something like sprintf()
After you get the number from the edit (use ES_NUMBER for numerical input only, or isdigit() on string)
atoi() or atof(), make sure you cast correctly
then sprintf() back to text to set into the edit.
sprintf(sBuffer,"%02.2f",fNum);