Click to See Complete Forum and Search --> : Read-only edit box problem


Coder Noob
September 24th, 2006, 03:47 PM
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.

golanshahar
September 24th, 2006, 04:31 PM
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

Coder Noob
September 24th, 2006, 07:16 PM
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?

golanshahar
September 25th, 2006, 02:40 AM
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

Viorel
September 25th, 2006, 03:52 AM
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.

VladimirF
September 25th, 2006, 02:23 PM
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???

golanshahar
September 25th, 2006, 04:01 PM
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

ovidiucucu
September 26th, 2006, 01:43 AM
Just take a look at this FAQ (http://www.codeguru.com/forum/showthread.php?t=350435).