Solved

Best way to append text to CEdit control?

Posted on 1998-11-19
9
3,929 Views
Last Modified: 2013-11-20
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.
0
Comment
Question by:TylerRick
9 Comments
 
LVL 2

Expert Comment

by:cyrilbdt
ID: 1325020
try to send WM_KEYDOWN with VK_END and then ReplaceSel.

hope this helps
0
 
LVL 2

Expert Comment

by:wyy_cq
ID: 1325021
i think this is not the best way . :-(

CString szT;
ed.GetWindowText(szT);
szT+="abc";
ed.SetWindowText(szT);

but perhaps this is the best way ,because it is very simple.
why not you think there need many steps.
0
 
LVL 2

Expert Comment

by:cyrilbdt
ID: 1325022
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...
0
 
LVL 8

Expert Comment

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

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

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" ) ;


0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Expert Comment

by:Malek103197
ID: 1325024
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.SetFocus();
      m_Edit.SetSel(nLen, nLen);
      m_Edit.ReplaceSel(pText);
}


0
 
LVL 7

Expert Comment

by:psdavis
ID: 1325025
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.

Phillip
0
 

Author Comment

by:TylerRick
ID: 1325026
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.
0
 
LVL 15

Accepted Solution

by:
Tommy Hui earned 50 total points
ID: 1325027
Assuming

  CEdit* editCtrl = // is valid

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


0
 

Author Comment

by:TylerRick
ID: 1325028
Good, I didn't know about GetWindowTextLength(). Thanks for your help.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Add content to output file 4 62
not able to insert into temp table 68 157
iSeries DB2 Query 2 91
sameEnds challenge 3 136
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now