ak44
ak44

Reputation: 11

win32 edit box displaying in new lines

My problem is with simple edit box. It is defined this way:

hEditIn=CreateWindowEx(WS_EX_CLIENTEDGE,
            L"EDIT",
            L"",
            WS_CHILD|WS_VISIBLE|ES_MULTILINE|
            ES_AUTOVSCROLL|ES_AUTOHSCROLL,
            50,
            120,
            400,
            200,
            hWnd,
            (HMENU)IDC_EDIT_IN,
            GetModuleHandle(NULL),
            NULL);

After that, when i call SendMessage like this:

SendMessage(hEditIn,
                            WM_SETTEXT,
                            NULL,
                            (LPARAM)L"Connected\r\n");

SendMessage(hEditIn,
                            WM_SETTEXT,
                            NULL,
                            (LPARAM)L"TESTSTR");

I get only last message instead of first message and second in new line.

This is also problematically because I want to display "Connected" every time in new line if serv retreive WM_ACCEPT message.

Upvotes: 1

Views: 3538

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 595377

WM_SETTEXT replaces the entire contents of the Edit control with the new text. To append new text onto the existing text, use EM_SETSEL to move the caret to the end of the existing text, then use EM_REPLACESEL to insert the new text at the current caret position.

void appendTextToEdit(HWND hEdit, LPCWSTR newText)
{
    int TextLen = SendMessage(hEdit, WM_GETTEXTLENGTH, 0, 0);
    SendMessage(hEdit, EM_SETSEL, (WPARAM)TextLen, (LPARAM)TextLen);
    SendMessage(hEdit, EM_REPLACESEL, FALSE, (LPARAM)newText);
}

appendTextToEdit(hEditIn, L"Connected\r\n");
appendTextToEdit(hEditIn, L"TESTSTR");

Upvotes: 3

Related Questions