-
Win32 sums
Hi all,
I am making the transition from console apps to GUIs, and have managed to create a windowed application which opens a dialog with a two text inpit spaces and an 'add' button plus a Listbox intended to show the result of an addition of the two previously entered numbers. Here is the code I have for the addition sun when 'add' is pressed:
Code:
case IDC_ADD: //Pressing the 'add' button.
{
BOOL bSuccess;
int first = GetDlgItemInt(hwnd, IDC_TEXT, &bSuccess, FALSE); //Getting number from first box.
int second = GetDlgItemInt(hwnd, IDC_TEXT2, &bSuccess, FALSE); //Getting number from second box.
int result = first + second; //Adding those together.
break;
}
Now, how to I get the result to be applied to the Listbox?
Thanks.
-
Well just use the listbox message LB_ADDSTRING to do it; that's described as:
Code:
LB_ADDSTRING
wParam=0;//not used; must be zero
lParam=(LPARAM)(LPCTSTR)lpsz;//address of string to add
In addition you can select the last inserted value to ensure that it is visible using LB_SETCURSEL, specifying the last inserted index. Let me implement a sample code with it:
Code:
char bff[100];
int li;
sprintf(bff,"%d",result);
li=SendMessage(listbox_handler,LB_ADDSTRING,0,(LPARAM)bff);
SendMessage(listbox_handler,LB_SETCURSEL,(WPARAM)li,0);
Hope that helps
Niara
-
Thanks - I used this code:
Code:
case IDC_ADD: //Pressing the 'add' button.
{
int len = GetWindowTextLength(GetDlgItem(hwnd, IDC_TEXT));
int len2 = GetWindowTextLength(GetDlgItem(hwnd, IDC_TEXT2));
char* buf;
char* buf2;
buf = (char*)GlobalAlloc(GPTR, len + 1);
buf2 = (char*)GlobalAlloc(GPTR, len2 + 1);
GetDlgItemText(hwnd, IDC_TEXT, buf, len + 1);
GetDlgItemText(hwnd, IDC_TEXT2, buf2, len2 + 1);
Third=atof(buf)+atof(buf2);
char strFirst[100];
sprintf(strFirst,"%0.0f",Third);
SetDlgItemText(hwnd, IDC_TEXT3, strFirst);
break;
-
"...buf = (char*)GlobalAlloc(GPTR, len + 1);..."
Also remember to call GlobalFree when you have finished to work with the mallocated buffers.
Glad to help
Niara
-
I would not dynamically alloc memory for this, much more complex than required.
I would just use a static buffer and ensure the edit controls had the ES_NUMBER style.
Code:
[char szBuf[MAX_PATH] = {0};
If I was worried that the text entered would exceed the buffer length I would call EM_SETLIMITTEXT in the WM_CREATE/WM_INITDIALOG msg (and limit it to MAX_PATH-1).
-
I think the same as novacain except in one thing: ES_NUMBER won't let the user enter point, coma or apostrophe characters to notate a floating point value, while george7378 is expecting to get a textual value and convert it to float (atof).
Niara