Problem to append text to an editbox
Hi, I created a simple window with a multiline Edit Control:
Code:
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):
Code:
SendMessage(GetDlgItem(hWnd, EDIT), EM_REPLACESEL, 0, (LPARAM)TEXT("\r\nSome text"));
if (GetLastError()) {
/* Error 5 ERROR_ACCESS_DENIED */
}
Same problem with EM_SETSEL:
Code:
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:
Code:
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!
Re: Problem to append text to an editbox
Quote:
Originally Posted by
mariop
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
Re: Problem to append text to an editbox
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)), it seems it's not the case of your program.
Just call SetLastError(0) before SendMessage to be sure that last error is set.
Re: Problem to append text to an editbox
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
Re: Problem to append text to an editbox
Quote:
Originally Posted by
mani3355
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. :(