NO

Author Topic: Adding lines to an edit control  (Read 7632 times)

Yates

  • Guest
Adding lines to an edit control
« on: February 23, 2007, 08:45:03 AM »
Dear all,

I have a little problem with an edit control.
I'd like to add text and add lines. "Add" means appending text to the text already in there.

Adding text: This can be done using EM_Replacesel, and making sure the selection corresponds to nothing but the end of the text that's already in there.
It works in principle by this function:

int Edit_AddText(HWND hEdit, char* txt)
{
    //deselect everything furst
       
    SendMessage(hEdit,
            (UINT) EM_SETSEL,
            (WPARAM) -1,
            (LPARAM) 0);
   
   
    SendMessage(hEdit,
            (UINT) EM_REPLACESEL,
            (WPARAM) FALSE,
            (LPARAM) txt);
}


So if I want to add a line, rather than just text, I thought this should work:

int Edit_AddLine(HWND hEdit, char* txt)
{
             
    char* Newline = "\n"; // or "\n\r" or "\r" etc
    Edit_AddText(hEdit, txt);
    Edit_AddText(hEdit, Newline);         
}


But it does NOT work correctly. The "\n" (or any combi of "\n" and/or "\r" gives a weirdo character in the edit. That's annoying. I also tried forcing ascii(13) and/or ascii(10) into the strings with code, but the result is the same :cry: . The Edit simply seems resistant to such characters.

How do I get around it?

Many thanks for your help.
Yates

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Adding lines to an edit control
« Reply #1 on: February 23, 2007, 09:46:35 AM »
What simply adding an empty string like ""?
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

JohnF

  • Guest
Adding lines to an edit control
« Reply #2 on: February 23, 2007, 12:44:55 PM »
"\r\n" works.

I tried this

Code: [Select]

SetWindowText(ehwnd, "Qwerty");
int lResult = GetWindowTextLength(ehwnd);
  SendMessage(ehwnd, (UINT) EM_SETSEL, (WPARAM)lResult, (LPARAM) -1);
  SendMessage(ehwnd,(UINT) EM_REPLACESEL,(WPARAM) FALSE,(LPARAM) "\r\nFred");


John

Yates

  • Guest
Adding lines to an edit control
« Reply #3 on: February 23, 2007, 01:33:10 PM »
Oooooh,

JohnF you are in fact right, "\r\n" seem to be the key to success (hey, I thought it was either deodorant or a fancy iPod).

Considering that I wrote that I had tried any combi of \n and \r it is rather dumb that I had not tried \r\n  :oops:

Many thanks and have a nice weekend
Y.

ivanhv

  • Guest
Adding lines to an edit control
« Reply #4 on: March 10, 2007, 12:19:50 AM »
You know:

CR (carriage return) = 0x13 = \r
LF (line feed) = 0x10 = \n

and the line ending protocols for three main systems:

MSDOS/WINDOWS: CR+LF
UNIX: LF
MAC: CR

Haven't you ever downloaded a source file from Internet and, when opened into notepad, it seemed as if the writer of the file had his/her enter key broken? The reason is that the original programmer surelly had his/her programming session in a UNIX system, and end of lines are encoded with only LF, that doesn't conform to Windows criteria of CR+LF.

Controls (and any other thing) in Windows will look for CR+LF (there is some exception although, where only LF is enough)