The CEdit control in MFC cannot cancel the Select All state solution using SetSel

Source: Internet
Author: User

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

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.