Help with Text Fields?

So, I am wondering, how do you save the input in a text field. For instance, say I want to have the user enter two values in the window, and then add them up and display them. How would I do that? WM_COMMAND?


ALSO: Can you help me with another problem? Here:
http://www.cplusplus.com/forum/windows/127064/

I know I am not supposed to advertise other threads, but this is just kind of halting me with jobs. Any help?
WM_GETTEXT or WM_SETTEXT with SendMessage()
Alternatively, GetWindowText() or SetWindowText().
How would I use that? Obviously in the messages, like this:

1
2
3
4
case WM_SETTEXT:
      //what code?
      break;
//other messages 


That IS right, right?
I usually use Get/SetWindowText().

There are a couple steps involved here. It takes two or vthree lines of code. To put text into an edit control you need the HWND of the edit control. When you created the edit control the HMENU parameter was the Control ID you assigned to the edit control such as ...

#define IDC_MY_TEXT_BOX_1 1500
#define IDC_MY_TEXT_BOX_2 1505

You can retrieve the HWND from the return of the original CreateWindow() call with the GetDlgItem() Api function. You feed into that function the HWND of the 'parent' of the edit control, i.e., possibly your main window, and the Control ID of the edit bcontrol for which you want the text and its HWND ...

1
2
3
TCHAR szBuffer[]=_T("Here Is Text I Want To Put In Edit Control");
HWND hEdit=GetDlgItem(hMainWindow, IDC_MY_TEXT_BOX_1);
SetWindowText(hEdit,szBuffer);


To retrieve text from an edit control its about the reverse of that. First get the HWND of the edit control from which you want the text. Then create a text buffer into which the call to GetWindowText() will write the text string...

1
2
3
TCHAR szBuffer[512];
HWND hEdit=GetDlgItem(hParent,IDC_MY_TEXT_BOX_1);
GetWindowText(hEdit,szBuffer,511);


Now, you can refine that by first calling GetWindowTextLen() to first determine how much text is there, and program accordingly. But that may involve memory allocations and a bit more complexity.
Wait, so I make a text box, with GetDlgItem. I got that. But... I thought GetDlgItem() is for dialogs. You can use them for regular windows?
Yes. Very handy and easy to use little function. No need to ever make globals out of child window controls, because control ids serve as proxies for them. If you know the parent HWND and the child window identifier, you can get the HWND of the child window control and manipulate it.
Topic archived. No new replies allowed.