Skip to content
Search

CEdit

lastmod 2021-02-25created 2016-11-164 edits

The CEdit class provides the functionality of a Windows edit control. An edit control is a rectangular child window in which the user can enter text.

Basic usage

Get the string in a CEdit.

CEdit::GetWindowText();

Get the length of the string in a CEdit.

CEdit::GetLength();

Enter a string into a CEdit.

CEdit::SetWindowText(text);

Scroll automatically when entering a string into a CEdit.

int nLength = m_pEdit->GetWindowTextLength();
CEdit::SetSel(nLength, nLength);

Clear all contents of a CEdit.

CEdit::SetSel(0, -1);
CEdit::Clear();

Total number of lines of the string in a CEdit.

CEdit::GetLineCount();

Number of characters in a line.

CEdit::LineLength(linenumber);

Get the index of the first character of a given line.

CEdit::LineIndex(linenumber);

Add characters at the very beginning of the CEdit box.

CEdit::SetSel(0, 0);
CEdit::ReplaceSel(text);

Set the string capacity of a CEdit. //(can be set up to 65536)//

CEdit::SetLimitText(size);

Set read-only.

CEdit::SetReadOnly(boolean);

How to scroll automatically

To scroll automatically to the last line in a multiline edit control, do the following.

CEdit::ReplaceSel();
CEdit::LineScroll(CEdit::GetLineCount());

Text Get/Set

CString str;
GetDlgItem(IDC_EDIT1)->GetWindowText(str);
GetDlgItem(IDC_EDIT3)->SetWindowText(str);

DDX Example

When using DDX, it can be used as follows.

void CLiveDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
    // ...
    DDX_Control(pDX, IDC_EDIT_LOG, m_edit2);
}
// ...
UpdateData();
m_edit2 = m_edit1;
UpdateData(FALSE);

Troubleshooting

Solutions to problems when using the CEdit class

  • If the 'Enter Key' cannot be entered when the multiline (ES_MULTILINE) property is set, add ES_WANTRETURN to the style information.

See also

Favorite site

author Zer0 sitemap