• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 376
  • Last Modified:

How to make text in edit control overwriteable

How do I make a edit control such that the text inside
can be overwritten. Like what happens in text editiors.
  • 4
1 Solution
Edit controls can already work that way.  In fact the MFC multipad example (ike notepad) uses an edit control as a view.

What do you mean by overwritten? Edit controls by default select all of the text inside them when they get focus. Typing anything while it has focus will remove the selection and thus the contents of the edit control.
kumaranaAuthor Commented:
I think I did not explain by problem compeletly.I want overwrite feature to happen without a selection being made. For example in Wordpad I press the INS key it starts overwriting text.
Now I have implemented by subclassing the edit and on OnChar() function if I find the cursor is not at end of the entered text, then I send VK_DELETE and then call base class CEdit::OnChar() something like below

  // get caret pos
  CPoint p = GetCaretPos();

  // get position of caret in edit box
  int pos = CharFromPos(P);

  // Get length of text
  GetDlgItemText( CurrData);
  int len = strlen( CurrData );

  if ( pos < len ) {
    SendMessage( KEY_DOWN, VK_DELETE );
Is there any other method?
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

This might work in OnChar(...):

   if (isprint(nChar)) {
       int nStart,nEnd;
       if (nEnd == nStart) {

An alternative is to use a rich-edit control instead of an edit control (set it to plain text mode).  This supports insert/overwrite mode.

However, I am not sure how to detect or get the overwrite mode without doing it yourself.

I'd suggest you subclass the control to include a bool indicating overwrite mode.  Init this to false.  In your OnChar, detect the VK_INSERT key.  If found, toggle the state of the flag.  You can then add a 'SetOverwriteMode' function that checks the flag, and if not set to overwrite, sends a VK_INSERT to the control.  Similarly for detecting and setting insert mode

BTW: my comment about the multipad sample is incorrect.  It does NOT support overwrite mode (nor does Notepad).  It appears only rich-edit controls natively support insert/overwrite mode.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now