Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Edit box highlights text when initially set

I have a dialog box with an edit control created using the CreateWindowEx function.

CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "", WS_CHILD | WS_VSCROLL | WS_EX_OVERLAPPEDWINDOW | WS_TABSTOP | WS_VISIBLE | ES_LEFT | ES_MULTILINE | ES_WANTRETURN | ES_AUTOVSCROLL ,
17, 40, 200, 220, parentHWND, (HMENU)IDC_EDIT1, GetModuleHandle(NULL), NULL);


I then use SendMessage( hwnd, WM_SETTEXT .... ) function to set the text in the edit box.
SendMessageW(hwnd, WM_SETTEXT, 0,(LPARAM)theText );


The problem is that the text gets highlighted which i do not want.

I've tried doing

SendMessage(hwnd,EM_SETSEL,-1,0);

but it still does not clear the selection. Is there a way to do this or what am i doing wrong?

Tony

Comments

  • AsmGuru62AsmGuru62 Member Posts: 6,519
    [color=Blue]If you send EM_SETSEL from WM_INITDIALOG handler, then Windows will set the focus to it when dialog is displayed. You have to delay that message, so it will happen after dialog is initialized and displayed. This is how to do it:

    1. Declare a user message. User messages begin with WM_USER and add some value to it, because Windows uses some of the range after that. I usually use the 0x4000 constant for it (see code below).

    2. Before WM_INITDIALOG ends you must post (not send) that message to the dialog handle. This will make this message to come to the dialog AFTER all other messages being done.

    3. In the same dialog procedure catch the message you just posted and send EM_SETSEL there.

    Here is some pseudo-code:[/color]
    [code]
    #define WM_DELAYED_DESELECT (WM_USER + 0x4000)

    BOOL CALLBACK YourDlgProc (HWND h, UINT m, WPARAM wp, LPARAM lp)
    {
    switch (m)
    {
    case WM_DELAYED_DESELECT:
    [color=Green]// send EM_SETSEL here[/color]
    break;

    case WM_INITDIALOG:
    [color=Green]// your initialization code... SetWindowText, etc.[/color]

    PostMessage (h, WM_DELAYED_DESELECT, 0, 0);
    break;

    case WM_COMMAND:
    [color=Green]// ... etc.[/color]
    break;

    default: return FALSE;
    }
    return TRUE;
    }
    [/code]
  • theexplorer99theexplorer99 Member Posts: 2
    Thanks for your help. That worked!
Sign In or Register to comment.