Click to See Complete Forum and Search --> : Problem to append text to an editbox


mariop
August 3rd, 2010, 07:33 AM
Hi, I created a simple window with a multiline Edit Control:

Edit = CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("EDIT"), NULL,
WS_CHILD | WS_VISIBLE | ES_MULTILINE,
20, 200, 200, 200,
hWnd, (HMENU)EDIT, GetModuleHandle(NULL), NULL);

If I set text using a WM_SETTEXT message, I don't get errorrs, but if I use EM_REPLACESEL I get Error 5 (ERROR_ACCESS_DENIED):

SendMessage(GetDlgItem(hWnd, EDIT), EM_REPLACESEL, 0, (LPARAM)TEXT("\r\nSome text"));
if (GetLastError()) {
/* Error 5 ERROR_ACCESS_DENIED */
}

Same problem with EM_SETSEL:

SendMessage(GetDlgItem(hWnd, EDIT), EM_SETSEL, (WPARAM)(0),(LPARAM)(-1));
SendMessage(GetDlgItem(hWnd, EDIT), EM_REPLACESEL, 0, (LPARAM)TEXT("\r\nSome text"));
if (GetLastError()) {
/* Error 5 ERROR_ACCESS_DENIED */
}

I noticed that if I send a WM_SETFOCUS message before the EM_REPLACESEL there are no error:

SendMessage(GetDlgItem(hWnd, EDIT), WM_SETFOCUS, (WPARAM)GetDlgItem(hWnd, EDIT), 0);
SendMessage(GetDlgItem(hWnd, EDIT), EM_REPLACESEL, 0, (LPARAM)TEXT("\r\nSome text"));
if (GetLastError()) {
/* NO ERRORS */
}

How can I resolve this problem? Do I have to send a WM_SETFOCUS message before the EM_REPLACESEL one every time I want to append some text to my Editbox?

Thanks for help!

VictorN
August 3rd, 2010, 10:15 AM
How can I resolve this problem? Do I have to send a WM_SETFOCUS message before the EM_REPLACESEL one every time I want to append some text to my Editbox?

Thanks for help!No, you should not send WM_SETFOCUS message at all!
EM_REPLACESEL is a notification message sent by WIndows after focus has been already set...
You should call SetFocus API instead. See this KB (http://support.microsoft.com/kb/109550)

ovidiucucu
August 3rd, 2010, 10:34 AM
Not all Windows API functions set the last error (value returned by GetLastError).
Although SendMessage can set last error (5, access denied) for some messages under Vista (see User Interface Privilege Isolation (UIPI) (http://blogs.msdn.com/b/vishalsi/archive/2006/11/30/what-is-user-interface-privilege-isolation-uipi-on-vista.aspx)), it seems it's not the case of your program.

Just call SetLastError(0) before SendMessage to be sure that last error is set.

mani3355
August 12th, 2010, 01:18 AM
hi,


I have to send a WM_SETFOCUS message before the EM_REPLACESEL one every time I want to append some text to my Editbox? pl. explained for me


regards,
phe9oxis,
http://www.guidebuddha.com

VictorN
August 12th, 2010, 01:32 AM
I have to send a WM_SETFOCUS message before the EM_REPLACESEL one every time I want to append some text to my Editbox? pl. explained for me
Just read the MSDN and Microsoft KB articles rather than post absolutely unrelated things to each thread in the Forum. :cool:

Note also that what you are doing since the last several hours may be assumed as SPAM. :(