Read-only edit box problem

I'm sure this question has been asked many times, but I can't find the exact answer I want anywhere. So... I'm making a read-only edit box in my program (for a log of what goes on in the program). Anyway, I want to be able to add text to the box when an event occurs. However, this does not mean that I want to be able to click on the box and type text into it. I simply want the program to add text to the box when an event takes place via SendMessage();

Here's what I try to do...

void createWindows(){

// creating window code

char test[] = "HI";

SendMessage(m_hLogWnd, WM_SETTEXT, 0, (LPARAM)test);

}

this should add text to the edit box, correct? It doesn't work for me.

Please tell me how to add text. Thanks.
[818 byte] By [Coder Noob] at [2007-11-20 1:37:35]
# 1 Re: Read-only edit box problem
The code you posted should work, if the m_hLogWnd is a valid handle to your edit box control it should set the text in the edit box to "HI".

One thing that is not clear is, when you say add do you want to append text to the edit control?

Cheers
golanshahar at 2007-11-9 13:24:55 >
# 2 Re: Read-only edit box problem
Well, by add text I mean just add it in a new line.

For example:

If this is what the edit box looks like

This is text

and I want to add more text, I want it to become this

This is text
This is more text

hopefully that clears it up

Edit: Well somehow I managed to get things to work, but the text only writes on one line. That is, if I add another line to write, it overwrites the first one.

This is what I mean

fnLog("How's it going");
fnLog("Not bad!");

looks like this in the edit window

Not bad!

how can I fix this?
Coder Noob at 2007-11-9 13:25:54 >
# 3 Re: Read-only edit box problem
First you need to set your edit box style to ES_MULTILINE and ES_WANTRETURN and use the following code:

char szCurrentText[MAX_PATH]={0};
::SendMessage(m_hLogWnd, WM_GETTEXT, sizeof(szCurrentText), (LPARAM)szCurrentText);

char test[] = "HI";
strcat(szCurrentText,"\r\n");
strcat(szCurrentText,test);

::SendMessage(m_hLogWnd, WM_SETTEXT, 0, (LPARAM)szCurrentText);

Cheers
golanshahar at 2007-11-9 13:26:52 >
# 4 Re: Read-only edit box problem
In addition to the previous correct solution proposed by golanshahar, I think you can reduce resource consumption in case of bigger amount of lines, if you simply move the caret position to the end using EM_SETSEL message, and then append the next line using EM_REPLACESEL message.

I hope this helps.
Viorel at 2007-11-9 13:27:57 >
# 5 Re: Read-only edit box problem
char szCurrentText[MAX_PATH]={0};
::SendMessage(m_hLogWnd, WM_GETTEXT, sizeof(szCurrentText), (LPARAM)szCurrentText);

I understand that this was just an example, but if you need to safely get text from the window, you could use WM_GETTEXTLENGTH first to see how much space to allocate.
Also, out of curiosity - why MAX_PATH?
VladimirF at 2007-11-9 13:28:55 >
# 6 Re: Read-only edit box problem
Also, out of curiosity - why MAX_PATH?

If I would have wrote 260 would it be better? :D

Anyway its just an example no matter what constant I would use it wont be accurate unless you know that your edit wont exceed some MAX number of characters.

But to be on the safe side using WM_GETTEXTLENGTH would be better :)

Cheers
golanshahar at 2007-11-9 13:29:53 >
# 7 Re: Read-only edit box problem
Just take a look at this FAQ ( http://www.dev-archive.com/forum/showthread.php?t=350435).
ovidiucucu at 2007-11-9 13:30:56 >