• Announcements

Archived

This topic is now archived and is closed to further replies.

I'm trying to read user input in a window

Recommended Posts

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,
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?

Share on other sites
Lookup GetWindowTextLength, GetWindowText and/or WM_GETTEXT

Share on other sites
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]

Share on other sites
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);}

Share on other sites
quote:

if(lpszEdit == "10")

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

Qui fut tout, et qui ne fut rien

[edited by - Invader X on July 5, 2003 4:27:39 PM]

Share on other sites
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
}

Share on other sites
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]

Share on other sites
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]

Share on other sites
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]

Share on other sites
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.

~

Share on other sites
Thanks for your help Zern & Drekkar. Should I need to try your method out Zern becuase the other method should work fine? Although it still doesn''t.

quote:
Original post by drekkar
im suprised that compiled you have too many )''s in the if line, unless you retyped it and typo''d ._.;>

Yes I must have retyped it. I looked back at my code and noticed I''d put too many brackets in there.

quote:
Original post by drekkar
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

I''ve also never used MFC so found it a bit strange what Zern was saying. All been explained of course. :D

Well I guess I''ll just have a play around with the code and see if I can fathom out why lpszEdit isn''t recieving any info from the GetDlgItemText function. I''ll probably just end up making the main window one big dialog box. Is this crazy or the normal way windows should be created?

• Forum Statistics

• Total Topics
627726
• Total Posts
2978816

• 10
• 9
• 21
• 14
• 12