4

I have Created an Edit window. I want one string to be displayed in one line and the other string to be displayed on the other line, but the code I am executing only displays the second string. below is my code snippet:

hWndEdit = CreateWindow("EDIT", // We are creating an Edit control
                                NULL,   // Leave the control empty
                                WS_CHILD | WS_VISIBLE | WS_HSCROLL |
                                WS_VSCROLL | ES_LEFT | ES_MULTILINE |
                                ES_AUTOHSCROLL | ES_AUTOVSCROLL,
                                10, 10,1000, 1000,
                                hWnd,
                                0,
                                hInst,NULL);
SetWindowText(hWndEdit, TEXT("\r\nFirst string\r\n"));

SetWindowText(hWndEdit, TEXT("\r\nSecond string"));

OUTPUT: OUTPUT

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Ayse
  • 2,676
  • 10
  • 36
  • 61
  • 3
    I suggest a thorough review of the [SetWindowText](http://msdn.microsoft.com/en-us/library/windows/desktop/ms633546(v=vs.85).aspx), especially the part that fundamentally describes how the function lives up to its namesake : SetWindowText(). Its not AppendWindowText(). Since this is an edit control I would surmise a visit to [Edit Control Messages](http://msdn.microsoft.com/en-us/library/windows/desktop/ff485923(v=vs.85).aspx) and the things they can do for you may be an interesting read. – WhozCraig Mar 16 '13 at 08:01
  • 1
    @WhozCraig It seems like I keep reading great comments from you. Please start posting them as answers so I can start upvoting them! – Cody Gray - on strike Mar 16 '13 at 08:10
  • 1
    @CodyGray Thanks, Cody. Your avatar is *awesome*, by-the-way. I've only been doing this for about 9 active months, but sometimes I just forget to post the answer as that. Or I don't feel it is really a solid contribution as an answer. There are times when RTFM is appropriate as such, but I just drop a comment. Habit, I guess. – WhozCraig Mar 16 '13 at 08:14

2 Answers2

9

You are only seeing the last line because SetWindowText() replaces the entire contents of the window in one go.

If you want to display both lines at one time, simply concatenate them together in a single call to SetWindowText():

SetWindowText(hWndEdit, TEXT("\r\nFirst string\r\n\r\nSecond string"));

On the other hand, if you want to insert them at different times, you have to use the EM_SETSEL message to place the edit caret at the end of the window and then use the EM_REPLACESEL message to insert text at the current caret position, as described in this article:

How To Programatically Append Text to an Edit Control

For example:

void AppendText(HWND hEditWnd, LPCTSTR Text)
{
    int idx = GetWindowTextLength(hEditWnd);
    SendMessage(hEditWnd, EM_SETSEL, (WPARAM)idx, (LPARAM)idx);
    SendMessage(hEditWnd, EM_REPLACESEL, 0, (LPARAM)Text);
}

.

AppendText(hWndEdit, TEXT("\r\nFirst string\r\n"));
AppendText(hWndEdit, TEXT("\r\nSecond string"));
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 2
    +1, especially for the constructive use of control messages (which I tried to not-so-shyly drop a hint to look at in the opening comment). – WhozCraig Mar 16 '13 at 08:17
2
hWndEdit = CreateWindow("EDIT", // We are creating an Edit control
                                NULL,   // Leave the control empty
                                WS_CHILD | WS_VISIBLE | WS_HSCROLL |
                                WS_VSCROLL | ES_LEFT | ES_MULTILINE |
                                ES_AUTOHSCROLL | ES_AUTOVSCROLL,
                                10, 10,1000, 1000,
                                hWnd,
                                0,
                                hInst,NULL);
        SetWindowText(hWndEdit, TEXT("\r\nFirst string\r\n\r\nSecond string"));

or

        SetWindowText(hWndEdit, TEXT("\r\nFirst string\r\n"));

        char* buf = malloc(100);
        memset(buf, '\0', 100);

        GetWindowText(hWndEdit, (LPTSTR)buf, 100);
        strcat(buf, "\r\nSecond string");
        SetWindowText(hWndEdit, (LPTSTR)buf); 
Motomotes
  • 4,111
  • 1
  • 25
  • 24