Q: How to replace a line in a multi-line edit control?
A: The example below shows how it can be done and to avoid often made mistakes.
Code:
void CMyDialog::ReplaceEditLineText(int nLine, LPCTSTR pszNewText)
{
// 'CEdit::LineIndex()' takes as parameter the index of the line
// and returns the character index of the line specified.
// The character index is is the number of characters from
// the beginning of the edit control to the specified line.
const int nLineStart = m_edit.LineIndex(nLine);
if(nLineStart != -1)
{
// 'CEdit::LineLength()' takes as parameter the character index
// of the line and not the line index as often is mistaken.
const int nLineLength = m_edit.LineLength(nLineStart);
// select the line, and replace the text.
m_edit.SetSel(nLineStart, nLineStart + nLineLength);
m_edit.ReplaceSel(pszNewText);
}
}