CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1
  1. #1
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Windows SDK Edit Control: How to append text to an edit control?

    Q: Which is the best method to append text to an edit control?

    A: One method is to call 'GetWindowText()' to get the initial text, append the new text to it, then put it back with 'SetWindowText()'.
    This works, but is not practical, especially if the text lenght is huge and the appending is made very frequently.

    A better solution is to put the edit selection at the end of the initial text, then simply replace the selection with the text to append:


    • 'Win32 API'

      Code:
      void AppendTextToEditCtrl(HWND hWndEdit, LPCTSTR pszText)
      {
         int nLength = GetWindowTextLength(hWndEdit); 
         SendMessage(hWndEdit, EM_SETSEL, (WPARAM)nLength, (LPARAM)nLength);
         SendMessage(hWndEdit,EM_REPLACESEL, (WPARAM)FALSE, (LPARAM)pszText);
      }
    • 'Win32 API' (using macros defined in windowsx.h)

      Code:
      #include <windowsx.h>
      // ...
      void AppendTextToEditCtrl(HWND hWndEdit, LPCTSTR pszText)
      {
         int nLength = Edit_GetTextLength(hWndEdit); 
         Edit_SetSel(hWndEdit, nLength, nLength);
         Edit_ReplaceSel(hWndEdit, pszText);
      }



    Last edited by Andreas Masur; July 25th, 2005 at 03:35 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured