You are currently offline, waiting for your internet to reconnect

How To Programatically Append Text to an Edit Control

This article was previously published under Q109550
Windows-based applications often use edit controls to display text. Theseapplications sometimes need to append text to the end of an edit controlinstead of replacing the existing text. There are two different ways to dothis in Windows:
  • Use the EM_SETSEL and EM_REPLACESEL messages.
  • Use the EM_SETSEL message with the clipboard functions to append text to the edit control's buffer.
NOTE: Because the message parameters for the EM_SETSEL message aredifferent between the 32-bit version and the 16-bit version, the followingcode 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 aWindows edit control. If the starting and ending positions of the range areset to the same position, no selection is made and a caret can be placed atthat position. To place a caret at the end of the text in a Windows editcontrol 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 theEM_REPLACESEL to append text to the edit control. An application sendsan EM_REPLACESEL message to replace the current selection in an editcontrol with the text specified by the lpszReplace (lParam) parameter.Because there is no current selection, the replacement text is inserted atthe current caret location. This example sets the selection to the end ofthe 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 Windowsclipboard. If the application has the clipboard open or finds it convenientto open the clipboard, and copies the text into the clipboard, then it cansend 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 ofthe edit control text using the EM_SETSEL message. Below is "pseudo" codethat 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 thatthe data in the clipboard must be in CF_TEXT format.

Article ID: 109550 - Last Review: 07/11/2005 22:36:00 - Revision: 1.3

Microsoft Platform Software Development Kit-January 2000 Edition, Microsoft Windows Software Development Kit 3.1

  • kbhowto kbeditctrl kbctrl KB109550