Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

Chapter 16: Text-Based Controls Visual C++ and MFC Fundamentals


The nID argument is the identifier of the control whose text you want to retrieve. The
lpStr or the rString is the returned value of the text in the edit box. It must be provided as
a string variable. If you are using a null-terminated variable, pass a third argument as
nMaxCount that specifies the maximum length of the string. Here is an example:

void CFormView1View::OnButton1()
{
// TODO: Add your control notification handler code here
CString Edit1, Edit2, Result;

GetDlgItemText(IDC_EDIT1, Edit1);
GetDlgItemText(IDC_EDIT2, Edit2);
Result = Edit1 + " " + Edit2;

SetDlgItemText(IDC_EDIT3, Result);
}

As mentioned already, an edit box can be used to request information from the user. If
you want to prevent the user from changing the value of an edit box, you can make it
read-only. This is taken care of by setting the Read-Only property to True or checking its
check box. To do this programmatically, call the CEdit::SetReadOnly() method.

If an edit box is not “read-only”, that is, if it allows the user to change its value, the user
must first give it focus. When an edit box has focus, it displays a blinking caret. By
default, the carret is an I beam. If you want to use a different caret, you have various
options. You can change the caret from an I beam to a wider or taller gray caret by
calling the CWnd::CreateGrayCaret() method. Its syntax is:

void CreateGrayCaret( int nWidth, int nHeight );

This method allows you to specify a width and a height for a gray blinking caret. After
creating the caret, to display it, call the CWnd::ShowCaret() method. Its syntax is:

void ShowCaret( );

Here is an example:

BOOL CSolidCaretDlg::OnInitDialog()
{
CDialog::OnInitDialog();

// TODO: Add extra initialization here
m_Username.CreateGrayCaret(5, 15);
m_Username.ShowCaret();

return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
Free download pdf