-2

I have been trying to create a new line in an Edit control. I used the sample code from Win32 - Appending text to an Edit Control with only a few small changes

To answer the question about multi-line, yes.

CreateWindow("EDIT","",WS_CHILD|WS_VISIBLE|ES_MULTILINE,
       0,0,300,500,hwnd,(HMENU)1,inst,NULL);

Here is the edited code

// get edit control from dialog
HWND hwndOutput = GetDlgItem( hwnd, IDC_OUTPUT );

// get the current selection
DWORD StartPos, EndPos;
SendMessage( hwndOutput, EM_GETSEL, reinterpret_cast<WPARAM>(&StartPos), reinterpret_cast<WPARAM>(&EndPos) );

// move the caret to the end of the text
int outLength = GetWindowTextLength( hwndOutput );
SendMessage( hwndOutput, EM_SETSEL, outLength, outLength );

//INSERTED CODE INSERTED CODE INSERTED CODE INSERTED CODE INSERTED CODE 
// insert newline
SendMessage( hwndOutput, EM_REPLACESEL, TRUE, reinterpret_cast<LPARAM>((TCHAR*)"\n\r") );
int outLength = GetWindowTextLength( hwndOutput );
SendMessage( hwndOutput, EM_SETSEL, outLength, outLength );
//INSERTED CODE INSERTED CODE INSERTED CODE INSERTED CODE INSERTED CODE 


// insert the text at the new caret position
SendMessage( hwndOutput, EM_REPLACESEL, TRUE, reinterpret_cast<LPARAM>(newText) );

// restore the previous selection
SendMessage( hwndOutput, EM_SETSEL, StartPos, EndPos );

The text is appending as expected, but there is still no newline. What am I missing?

Community
  • 1
  • 1
BSD
  • 329
  • 3
  • 13
  • 3
    Does the edit control have the `ES_MULTILINE` style set? If not, newlines won't have any effect. – Ken White May 04 '17 at 22:23
  • 2
    It is "\r\n" as in return – lmiguelmh May 04 '17 at 22:32
  • What do you mean "still"? "edited"? "question" ... are you referring to some "part 1" that I can't see? – Lightness Races in Orbit May 04 '17 at 22:34
  • 2
    Just an abbreviation for Not Right. "Right Now" might help. – Hans Passant May 04 '17 at 22:41
  • No, there is no part 1. I mean still as in I copied a section of code I mostly understand and I made one critical edit that I thought would work. No new line. I've been working at it for a while. As for the question, @KenWhite asked if I had multiline on, so I edited the question to answer him. – BSD May 05 '17 at 15:11

1 Answers1

1

Here's your code:

SendMessage( hwndOutput, EM_REPLACESEL, TRUE, reinterpret_cast<LPARAM>((TCHAR*)"\n\r") );

You wrote \n\r instead of \r\n.

Whoops.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • 4
    The `TCHAR*` typecast is also technically wrong. The code "worked" only because the OP is clearly creating an ANSI window instead of a Unicode window, and `TCHAR` is `char` when `UNICODE` is not defined. If the code is intended to remain ANSI-only, then just drop the `TCHAR*` typecast altogether: `reinterpret_cast("\r\n")`. Otherwise, use the `TEXT()` macro instead: `reinterpret_cast(TEXT("\r\n"))` – Remy Lebeau May 04 '17 at 23:05
  • 1
    `TEXT()` should also be applied to the string literals in the `CreateWindow()` call, eg: `CreateWindow(TEXT("EDIT"), TEXT(""), ...)`, and to any other `TCHAR`-based API functions being used. – Remy Lebeau May 04 '17 at 23:07
  • @RemyLebeau: Indeed. Please write an answer containing this information – Lightness Races in Orbit May 04 '17 at 23:13
  • Thanks. I was reading other sources that just said \n and I tried \r\n but apparently not with the current configuration. In any case, that is working now. Stupid on my part. Thanks, – BSD May 05 '17 at 15:08
  • @BSD: most standard controls like static, button, etc. only need `\n`. Edit and richedit controls are the exception that require `\r\n`. – zett42 May 05 '17 at 18:50
  • @zett42 Great. Thanks for the clarification. – BSD May 08 '17 at 13:29