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

Best way to append text to CEdit control?

What is the best way to append text to the end of a CEdit control?

I could use GetWindowText, add my new text to it, and then send it to SetWindowText. But that seems like too many steps.

I could use ReplaceSel, but that only works if the caret is at the end. If I do that, how do I set it to the end? I could do this:
 m_textExplanation.SetSel(m_textExplanation.GetLineCount() * m_textExplanation.LineLength(), m_textExplanation.GetLineCount() * m_textExplanation.LineLength());
 m_textExplanation.ReplaceSel("This is a test!\n");
But that seems like too much work, too.
1 Solution
try to send WM_KEYDOWN with VK_END and then ReplaceSel.

hope this helps
i think this is not the best way . :-(

CString szT;

but perhaps this is the best way ,because it is very simple.
why not you think there need many steps.
TylerRick use GetLineCount, wich means that it is multi line edit control (64K limit).
And if is happen frequently, you will transfer 20k to add 3 bytes...
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.

The GetWindowText/SetWindowText is easy, wrap into a function or derived class if you want to save some typing

class CXEdit : public CEdit
  void AppendText( const CString& strAppend )
         CString szT;
         szT+=strAppend ;
} ;

Provided you don't add any data members to CXEdit you can simply cast existing CEdit's, example (assume ed is a CEdit)

((CXEdit *)&ed)->AppendText( "abc" ) ;

Here is one way that works:
Pass the text you want to append into the function below and it will append to the existing text in the Edit control.

void CEditTextDlg::Appendtext(LPCSTR pText)
      int nLen = GetWindowTextLength();
      m_Edit.SetSel(nLen, nLen);

And here's my two cents worth.

I almost always create a Member Variable for a CEdit box.  (You would do it with the Member Variable tab in class wizard).  That will put a

CString m_strMyEditBox;

inside of your header file.

Then inside of the function you want to append your text...

CMyDialogBox::OnMyFunction( )
   UpdateData( TRUE );
// Now m_strMyEditBox has the contents of your string

   m_strMyEditBox += ".bmp";

   UpdateData( FALSE );

// And that's it.  The UpdateData combination will do all of the work for you.

TylerRickAuthor Commented:
Maybe that is the best way, wyy_cq. I was just wondering if any of those steps could be avoided.

It sounds like they can't be. But it might be helpful, like Answers2000 suggested, to use a wrapper function.

psdavis, the problem with that is you are too limited with just a CString. You don't have the flexibility that you have if you map it to a CEdit. With a CEdit you can use the member functions, too.
Tommy HuiEngineerCommented:

  CEdit* editCtrl = // is valid

  DWORD l = editCtrl->GetWindowTextLength();
  editCtrl->SetSel(l, l);

TylerRickAuthor Commented:
Good, I didn't know about GetWindowTextLength(). Thanks for your help.
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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

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