Thread: Detecting Edit Control Text

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    4

    Detecting Edit Control Text

    Hi, I'm on Windows XP SP2 using Visual C++ 2008 Express Edition.

    I'm having a small problem detecting the text in an edit control. I have it subclassed as so:

    Code:
    LRESULT CALLBACK EditProc (HWND hWndEdit, UINT msg, 
                               WPARAM wParam, LPARAM lParam)
    {
    	static TCHAR szBuffer[100];
    	if (msg == WM_KEYDOWN)
    	{	
    		switch (wParam)
    		{
    			case VK_RETURN:
    				Edit_GetText (hWndEdit, szBuffer, 100);
    				if (szBuffer == TEXT ("text"))
    				{
    					Beep (300, 100);
    				}
    				else
    				{
    					MessageBox (hWndEdit, szBuffer, NULL, MB_OK);
    				}
    				Edit_SetText (hWndEdit, TEXT(""));
    				PeekMessage (&message, hWndEdit, WM_KEYFIRST, WM_KEYLAST, PM_REMOVE);
    				break;
    		}
    	}
    	return CallWindowProc (wndProc, hWndEdit, msg, wParam, lParam) ;
    }
    Upon pressing the Enter key in run time with the Edit Control in focus, Edit_GetText should get the text in the Edit Control and copy it in szBuffer, and if szBuffer is "text", it should beep. However, during run time, when I typed in "text" (without the quotes) in the Edit Control and pressed enter, it instead goes to 'else' and displays what is in szBuffer in a message box, which verifies that szBuffer is "text".

    I suspected that maybe I needed to include an extra character(s) at the end of "text" in the if statement to include the Enter key. If that's the case can anybody tell me what other characters I need to include? Any help is appreciated.

    Entire Source (probably not needed in a problem like this, but just in case):
    Code:
    #include <windows.h>
    #include <Windowsx.h>
    #include "resource.h"
    
    LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
    LRESULT CALLBACK EditProc (HWND, UINT, WPARAM, LPARAM);
    
    HINSTANCE hInstance;
    static TCHAR szAppName[] = TEXT ("Project ZENNOU");
    WNDPROC wndProc;
    MSG message;
    
    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int iCmdShow)
    {
    	WNDCLASS wndclass;
    	HWND hWnd;
    
    	wndclass.lpszClassName = szAppName;
    	wndclass.lpfnWndProc = WndProc;
    	wndclass.style = CS_OWNDC | CS_VREDRAW | CS_HREDRAW;
    	wndclass.hInstance = hInstance;
    	wndclass.hIcon = LoadIcon (hInstance, MAKEINTRESOURCE(IDI_ICON));
    	wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
    	wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    	wndclass.lpszMenuName = MAKEINTRESOURCE(IDR_MYMENU);
    	wndclass.cbClsExtra = 0;
    	wndclass.cbWndExtra = 0;
    
    	RegisterClass (&wndclass);
    
    	hWnd = CreateWindow (szAppName, szAppName, WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
    						 CW_USEDEFAULT, CW_USEDEFAULT, 700, 500, NULL, NULL, hInstance, NULL);
    
    	ShowWindow (hWnd, iCmdShow);
    	UpdateWindow (hWnd);
    
    	while (GetMessage (&message, NULL, 0, 0))
    	{
    		TranslateMessage (&message);
    		DispatchMessage (&message);
    	}
    	
    	return message.wParam;
    }
    
    LRESULT CALLBACK WndProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
    	HDC hDC;
    	PAINTSTRUCT paintstruct;
    	static HWND hWndEdit;
    	TEXTMETRIC textmetric;
    	int cyChar;
    	HFONT hFont;
    
    	switch (msg)
    	{
    		case WM_CREATE:
    			hWndEdit = CreateWindow (TEXT ("EDIT"), NULL, WS_CHILD | WS_VISIBLE | WS_BORDER | 
    									 ES_AUTOHSCROLL | ES_MULTILINE, 0, 0, 0, 0, hWnd, (HMENU) ID_EDIT,
    									 ((LPCREATESTRUCT) lParam) -> hInstance, NULL);
    			SendMessage (hWndEdit, EM_LIMITTEXT, 100, 0);
    			hFont = CreateFont (15, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, TEXT("Verdana"));
    			SendMessage (hWndEdit, WM_SETFONT, (WPARAM)hFont, TRUE);
    			wndProc = (WNDPROC) SetWindowLong (hWndEdit, GWL_WNDPROC, (LPARAM) EditProc);
    			return 0;
    
    		case WM_PAINT:
    			BeginPaint (hWnd, &paintstruct);
    			EndPaint (hWnd, &paintstruct);
    			return 0;
    
    		case WM_COMMAND:
    			return 0;
    
    		case WM_SIZE:
    			hDC = GetDC (hWnd);
    			GetTextMetrics (hDC, &textmetric);
    			cyChar = textmetric.tmHeight;
    			MoveWindow (hWndEdit, 0, HIWORD (lParam) - (cyChar + 4), LOWORD (lParam), (cyChar + 4), TRUE);
    			return 0;
    
    		case WM_DESTROY:
    			ReleaseDC (hWnd, hDC);
    			PostQuitMessage (0);
    			return 0;
    
    		default:
    			return (DefWindowProc (hWnd, msg, wParam, lParam));
    	}
    
    }
    
    LRESULT CALLBACK EditProc (HWND hWndEdit, UINT msg, 
                               WPARAM wParam, LPARAM lParam)
    {
    	static TCHAR szBuffer[100];
    	if (msg == WM_KEYDOWN)
    	{	
    		switch (wParam)
    		{
    			case VK_RETURN:
    				Edit_GetText (hWndEdit, szBuffer, 100);
    				if (szBuffer == TEXT ("text"))
    				{
    					Beep (300, 100);
    				}
    				else
    				{
    					MessageBox (hWndEdit, szBuffer, NULL, MB_OK);
    				}
    				Edit_SetText (hWndEdit, TEXT(""));
    				PeekMessage (&message, hWndEdit, WM_KEYFIRST, WM_KEYLAST, PM_REMOVE);
    				break;
    		}
    	}
    	return CallWindowProc (wndProc, hWndEdit, msg, wParam, lParam) ;
    }
    Last edited by yoonkwun; 07-12-2009 at 02:07 AM.

  2. #2
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    Code:
    if (szBuffer == TEXT("text"))
    Yikes! That's not how you compare strings in C/C++ dude.
    Try this instead:-
    Code:
    if (!lstrcmp(szBuffer, TEXT("text")))

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    4
    Ah, now it works. Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Controlling edit control input
    By Gerread in forum Windows Programming
    Replies: 6
    Last Post: 05-03-2007, 08:56 PM
  2. Problems with my edit control...
    By tyouk in forum Windows Programming
    Replies: 19
    Last Post: 10-19-2003, 12:36 AM
  3. inserting text into edit control
    By bennyandthejets in forum Windows Programming
    Replies: 4
    Last Post: 01-14-2003, 05:38 AM
  4. adding a line of text to a readonly edit control?
    By Kibble in forum Windows Programming
    Replies: 2
    Last Post: 11-25-2002, 09:04 PM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 07:35 PM