I'm trying to read user input in a window

Started by
9 comments, last by Steve-B 20 years, 9 months ago
Hi folks I've created a win32 window with message handling and all that stuff. I used the CreateWindow function to create an edit box. here is the small piece of code:

hEdit = CreateWindow("EDIT", lpszEdit,
                                             WS_CHILD | WS_VISIBLE | 
                                             ES_LEFT | WS_BORDER,
                                             20, 40, 100, 20,
                                             hWnd,
                                             (HMENU)IDC_EDIT,
                                             hInst, NULL);
The problem I'm having is finding out what the user has entered in the edit box. I know about a similar function called GetDlgItemText which retrieves the text which has been entered. That function is only for Dialog boxes though and I'm just using a window without a dialog box. Any ideas?
Advertisement
Lookup GetWindowTextLength, GetWindowText and/or WM_GETTEXT
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
what's stopping you from using GetDlgItemText on the edit box?

GetDlgItemText(hwnd, IDC_EDIT, buffer, 256);


[edited by - drekkar on July 5, 2003 3:28:24 PM]
drekkar: I already tried that but it didn''t alter the variable so I assumed it would only work with dialog boxes. I''ll provide more code. The variable I''m trying to alter is called lpszEdit and it''s of the type LPTSTR. Here is the full WndProc code I''m using.

LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam){	static HWND hText = NULL;	static HWND hEdit = NULL; 	static HWND hButton = NULL;	switch(uMsg)	{		case WM_COMMAND:			switch(LOWORD(wParam))			{				case IDM_TEST:                        hText = CreateWindow("STATIC", "Static Text",                                             WS_CHILD | WS_VISIBLE | SS_LEFT,                                             20, 10, 100, 15,                                             hWnd,                                             (HMENU)IDC_TEXT,                                             hInst, NULL);                        hEdit = CreateWindow("EDIT", lpszEdit,                                             WS_CHILD | WS_VISIBLE |                                              ES_LEFT | WS_BORDER,                                             20, 40, 100, 20,                                             hWnd,                                             (HMENU)IDC_EDIT,                                             hInst, NULL);                        hButton = CreateWindow("BUTTON", "Push Button",                                             WS_CHILD | WS_VISIBLE |                                              BS_PUSHBUTTON,                                             20, 80, 100, 32,                                             hWnd,                                             (HMENU)IDC_BUTTON,                                             hInst, NULL);                        break;                case IDM_ABOUT:						lpszEdit = "10";						DialogBox(hInst, "AboutBox", hWnd, (DLGPROC)About);                        break;				case IDM_EXIT:                        DestroyWindow(hWnd);                        break;								case IDC_BUTTON:						GetDlgItemText(hWnd, IDC_EDIT, lpszEdit, 256);						if(lpszEdit == "10")							DestroyWindow(hWnd);						break;			}			break;		case WM_DESTROY:			PostQuitMessage(0);			break;		default:			return (DefWindowProc(hWnd, uMsg, wParam, lParam));	}	return (0L);}
quote:
if(lpszEdit == "10")


You cannot compare C-strings like this. Use strcmp().


Qui fut tout, et qui ne fut rien
Invader's Realm

[edited by - Invader X on July 5, 2003 4:27:39 PM]
the above post is correct about using strcmp() to compare string, but do not be fooled by the function

if(strcmp(string1, string2)){
// wrong
}

if(strcmp(string1, string2) == 0){
// the strings are equal
}
Yes I tried that second version of the strcmp function and it doesn't bring any errors but it also doesn't fix the problem. I type the word "Quit" in the edit box and press on the button and the window should be destroyed. The problem is that the string just isn't being written to the lpszEdit variable. I've checked using debug. Nothing is being stored in the string and I have no idea why.

case IDC_BUTTON:        GetDlgItemText(hWnd, IDC_EDIT, lpszEdit, 256);	if(strcmp(lpszEdit, "Quit")) == 0)	   DestroyWindow(hWnd);	break;


[edited by - Steve-B on July 5, 2003 4:59:18 PM]

[edited by - Steve-B on July 5, 2003 4:59:45 PM]
Hello Steve-B,


GetDlgItemText is used with MFC so unless you are using that on a CWnd, CWindow, or COleControlContainer derived object good luck having it work right. Now what you should do is this:

HWND hEdit = GetDlgItem(hDlg, IDC_EDIT);char Buffer[256];ZeroMemory(Buffer, sizeof(Buffer)); //Not sure if WM_GETTEXT clears this buffer but better safe than sorry.SendMessage(hEdit, WM_GETTEXT, (WPARAM)256, (LPARAM)Buffer);



Now Buffer should contain the text inside the edit box.


Edit: Source tags

[edited by - zern on July 5, 2003 5:20:56 PM]
quote:Original post by Steve-B
Yes I tried that second version of the strcmp function and it doesn't bring any errors but it also doesn't fix the problem. I type the word "Quit" in the edit box and press on the button and the window should be destroyed. The problem is that the string just isn't being written to the lpszEdit variable. I've checked using debug. Nothing is being stored in the string and I have no idea why.

case IDC_BUTTON:        GetDlgItemText(hWnd, IDC_EDIT, lpszEdit, 256);	if(strcmp(lpszEdit, "Quit")) == 0)	   DestroyWindow(hWnd);	break;


im suprised that compiled you have too many )'s in the if line, unless you retyped it and typo'd ._.;

if(strcmp(lpszEdit, "Quit") == 0)

i would like to add that i have never used MFC (and dont plan to ) and I can verify this method does work because its the only method I use to get text from edit boxes etc

[edited by - drekkar on July 5, 2003 5:24:23 PM]
Ah ... I looked at the documentation in the Platform SDK that comes with VS.NET in the index it only showed the "GetDlgItemText method" at the top which hid the actually "GetDlgItemText" selection which is not MFC only. My mistake.


~

This topic is closed to new replies.

Advertisement