Clear the text when edit receives focus

Field has default text 1

I have an edit with some default text. How do I clear the text when edit receives focus.

        hEdit=CreateWindowEx(WS_EX_CLIENTEDGE,
            "EDIT",
            "",
            WS_CHILD|WS_VISIBLE|
            ES_MULTILINE|ES_AUTOVSCROLL|ES_AUTOHSCROLL,
            5,
            85,
            200,
            30,
            hwnd,
            (HMENU)IDC_MAIN_EDIT,
            GetModuleHandle(NULL),
            NULL);
        HGDIOBJ hfDefault=GetStockObject(DEFAULT_GUI_FONT);
        SendMessage(hEdit,
            WM_SETFONT,
            (WPARAM)hfDefault,
            MAKELPARAM(FALSE,0));
        SendMessage(hEdit,
            WM_SETTEXT,
            NULL,
            (LPARAM)"Click here, before scan QR!");

Upvotes: 0

Views: 1896

Answers (2)

andlabs
andlabs

Reputation: 11588

The best way to do this is to send the text box the EM_SETCUEBANNER message, which sets a "cue banner". This is placeholder text that is automatically displayed by the operating system when the textbox is empty.

This will not only make your code simpler, but it will create a user experience that is more consistent with other applications, thus making your application easier to use.

Upvotes: 4

user1593881
user1593881

Reputation:

To clear the text when edit receives the focus use the SetWindowText() function while handling the EN_SETFOCUS notification:

case WM_COMMAND:
{
    if (HIWORD(wParam) == EN_SETFOCUS && LOWORD(wParam) == IDC_MAIN_EDIT)
    {
        SetWindowText(hEdit, 0);
    }
}
break;

Upvotes: 1

Related Questions