Issue background:
My dialog box contains only one CEdit control, and when the dialog is initialized, I load the contents of the external file into the edit control.
In the code I clearly called the CEdit SetSel method correctly, but when the dialog box is displayed, the CEdit control is displayed as a full selection instead of the one I want to display without the selection.
The code is as follows:
BOOL Cxxxdialog::oninitdialog ()
{
cdialog::oninitdialog ();
LoadFile ();
return TRUE;
}
Load file
void Cxxxdialog::loadfile (void)
{
if (M_strfilename.isempty ()))
return;
try{
CEdit *pedit = (CEdit *) GetDlgItem (idc_edit_content);
CStdioFile file (m_strfilename,cfile::moderead);
CString strfileline=_t ("");
int nlen=0;
while (file. ReadString (strfileline))
{
Strfileline + = _t ("\ n");
Nlen=pedit->getwindowtextlength ();
Pedit->setsel (Nlen,nlen);
Pedit->replacesel (Strfileline);
}
Pedit->setsel ( -1,0);
}
catch (CFileException *e) {
e->reporterror ();
E->delete ();
}
}
Very puzzled, tried a lot of methods, did not achieve the effect I want, after many times Google to find a solution to similar problems, in fact, is caused by the default characteristics of the CEdit, rather than CEdit SetSel method has a problem, in OnInitDialog () called SetSel ( -1,0) deselect is handled correctly by the system, but CEdit is the first focus of the dialog box control, so the dialog box appears CEdit get focus, CEdit control is the focus of the default processing method is to select all, so you see the above mentioned situation.
There are a number of workarounds:
1, CEdit of the TABSTOP do not choose.
2, join CEdit to get the Focus event processing
void Cxxxdialog::onsetfocusedit1 ()
{
((CEdit *) GetDlgItem (IDC_EDIT1))->setsel ( -1, 0, false);
}