Click to See Complete Forum and Search --> : Appending text to an edit control


dit6a9
August 13th, 2004, 05:29 PM
I wonder if I'm appending text to an edit control the right way.

BOOL CALLBACK dialogProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
HWND hwndText;
static char buffer[1024];
int i, iWindowTextLength;
char* sCurrentText;

...
hwndText = GetDlgItem(hwndDialog, 1);
iWindowTextLength = GetWindowTextLength(hwndText);

sCurrentText = (char*)malloc((iWindowTextLength+1)*sizeof(int));
GetDlgItemText(hwndDialog, 1, sCurrentText, iWindowTextLength+1);

sCurrentText = realloc(sCurrentText, (1024+iWindowTextLength+1)*sizeof(int));
...
strcat(sCurrentText, buffer);
SetDlgItemText(hwndDialog, 1, sCurrentText);
free(sCurrentText);
...
}

First I get the current length of the text. In order to store this text I allocate a memory block of iWindowTextLength+1 bytes. Then I change the length of this memory block in order to make room to append some text. The buffer variable contains the text to append. I append buffer the end of sCurrentText. Finally I use SetDlgItemText to update the edito control content.

Is everything ok this way? Is there a better way to append text to the current edit control content?

sbubis
August 15th, 2004, 02:02 AM
I am using approximately the same way since I didn't find something better. I mean Edit box by itself doesn't have a way to append text.
Just one note: don't forget to append \r\n combination to separate old and new parts (of course, if you need it).

dit6a9
August 15th, 2004, 03:42 AM
Many thanks. I found out another way to accomplish the same thing, and maybe it is a little bit better.

void AppendWindowText(HWND hWnd, const char* s)
{
int iLength;

iLength = GetWindowTextLength(hWnd);
SendMessage(hWnd, EM_SETSEL, iLength, iLength);
SendMessage(hWnd, EM_REPLACESEL, 0, (LPARAM)s);
SendMessage(hWnd, WM_VSCROLL, SB_BOTTOM, (LPARAM)NULL);

}

sbubis
August 15th, 2004, 04:13 AM
Thx
You're right. it's better.