Thread: Appending text to an edit control

  1. #1
    Registered User
    Join Date
    Jul 2004

    Appending text to an edit control

    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);
    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?

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Append text to edit control

    Your current method should work, but is a little less efficient. A couple of things you could think about:
    - Why are you using sizeof(int) when you are allocating chars?
    - Instead of reallocing, why not malloc as much memory as you need?
    - Error handling.

  3. #3
    Registered User
    Join Date
    Jul 2004
    I mistyped the int. You're right. It should be char. Many thanks. These are the classical problems when you practise "copy & paste" programming :-)

    I'm using realloc in order to make room to append more text but I guess you're right again, I can call malloc just once and allocate a memory block big enough store the final string.

    What do you really mean by error handling? Cheking GetLastError value? I must admit I am a little bit confused about error handling because I usually program in Java and I've grown accustomed to try...catch statements in order to control the exceptions. Now that I'm writing Windows applications in C I'm very sure about the best way to accomplish the same.

  4. #4
    Registered User
    Join Date
    Jul 2004
    Here is a function that I use to append text to a edit control if this helps any.

    void AppendWindowText(HWND hWnd, const char * lpString)
      int iLength = GetWindowTextLength(hWnd);
      SendMessage(hWnd, EM_SETSEL, iLength, iLength);
      SendMessage(hWnd, EM_REPLACESEL, 0, (LPARAM) lpString);
      SendMessage(hWnd, WM_VSCROLL, SB_BOTTOM, (LPARAM)NULL);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Controlling edit control input
    By Gerread in forum Windows Programming
    Replies: 6
    Last Post: 05-03-2007, 08:56 PM
  2. Problems with my edit control...
    By tyouk in forum Windows Programming
    Replies: 19
    Last Post: 10-19-2003, 12:36 AM
  3. adding a line of text to a readonly edit control?
    By Kibble in forum Windows Programming
    Replies: 2
    Last Post: 11-25-2002, 09:04 PM
  4. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 07:35 PM