HOWTO: Programatically Append Text to an Edit Control

Last reviewed: January 9, 1998
Article ID: Q109550
The information in this article applies to:
  • Microsoft Windows Software Development Kit (SDK), version 3.1
  • Microsoft Win32 Application Programming Interface (API) included with: - Microsoft Windows NT, versions 3.5, 3.51 - Microsoft Windows 95

SUMMARY

Windows-based applications often use edit controls to display text. These applications sometimes need to append text to the end of an edit control instead of replacing the existing text. There are several ways to do this in Windows:

  • Use the EM_SETSEL and EM_REPLACESEL messages.

-or-
  • Use the EM_SETSEL message with the clipboard functions to append text to the edit control's buffer.

MORE INFORMATION

NOTE: Because the message parameters for the EM_SETSEL message are different between the 32-bit version and the 16-bit version, the following code uses the Win32 macro to determine at build time if this is a 16- or 32- bit application.

The EM_SETSEL message can be used to place a selected range of text in a Windows edit control. If the starting and ending positions of the range are set to the same position, no selection is made and a caret can be placed at that position. To place a caret at the end of the text in a Windows edit control and set the focus to the edit control, do the following:

   HWND hEdit = GetDlgItem (hDlg, ID_EDIT);
   int ndx = GetWindowTextLength (hEdit);
   SetFocus (hEdit);
   #ifdef WIN32
      SendMessage (hEdit, EM_SETSEL, (WPARAM)ndx, (LPARAM)ndx);
   #else
      SendMessage (hEdit, EM_SETSEL, 0, MAKELONG (ndx, ndx));
   #endif

Once the caret is placed at end in the edit control, you can use the EM_REPLACESEL to append text to the edit control. An application sends an EM_REPLACESEL message to replace the current selection in an edit control with the text specified by the lpszReplace (lParam) parameter. Because there is no current selection, the replacement text is inserted at the current caret location. This example sets the selection to the end of the edit control and inserts the text in the buffer:

   #ifdef WIN32
      SendMessage (hEdit, EM_SETSEL, (WPARAM)ndx, (LPARAM)ndx);
   #else
      SendMessage (hEdit, EM_SETSEL, 0, MAKELONG (ndx, ndx));
   #endif
      SendMessage (hEdit, EM_REPLACESEL, 0, (LPARAM) ((LPSTR) szBuffer));

Another way to insert text into an edit control is to use the Windows clipboard. If the application has the clipboard open or finds it convenient to open the clipboard, and copies the text into the clipboard, then it can send the WM_PASTE message to the edit control to append text. Of course, any data that was in the clipboard will be lost.

Before sending the WM_PASTE message, the caret must be placed at the end of the edit control text using the EM_SETSEL message. Below is "pseudo" code that shows how to implement this method:

   OpenClipBoard () ;
   EmptyClipBoard() ;
   SetClipBoardData() ;

   #ifdef WIN32
      SendMessage (hEdit, EM_SETSEL, (WPARAM)ndx, (LPARAM)ndx);
   #else
      SendMessage (hEdit, EM_SETSEL, 0, MAKELONG (ndx, ndx));
   #endif
      SendMessage (hEdit, WM_PASTE,   0, 0L);

This "pseudo" code appends text to the end of the edit control. Note that the data in the clipboard must be in CF_TEXT format.
Keywords          : UsrCtl
Version           : WIN3.X:3.0,3.1,4.0;WINNT:3.5,3.51;
Platform          : Win95 winnt
Issue type        : kbhowto


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: January 9, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.