CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2010
    Posts
    2

    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!

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Problem to append text to an editbox

    Quote Originally Posted by mariop View Post
    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
    Victor Nijegorodov

  3. #3
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,244

    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.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  4. #4
    Join Date
    Aug 2010
    Posts
    51

    Smile 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

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Problem to append text to an editbox

    Quote Originally Posted by mani3355 View Post
    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.

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured