Select All and cancel all in the edit box.

Source: Internet
Author: User

For an edit box, select all and deselect all (prerequisite: the edit box gets the Focus)

Create a new dialog based MFC application named "d1". The interface is as follows:

Add a member variable bool m_bselectall for the dialog box class to control the full selection status. In addition, to overload the pretranslatemessage event of the dialog box class, add the followingCode:

 
Bool cd1dlg: pretranslatemessage (MSG * PMSG) {If (getasynckeystate (vk_control) & 0x8000) & (getasynckeystate ('A') & 0x8000) & PMSG-> hwnd = m_pedit-> m_hwnd) {If (m_bselectall) {m_pedit-> setsel (-1 ); // cancel all selection} else {m_pedit-> setsel (0,-1); // select all} m_bselectall =! M_bselectall; return true;} return cdialog: pretranslatemessage (PMSG );}

In this way, when the edit box gets the focus, if you press Ctrl + A, the content in the edit box will be "all selected" or "unall selected ".

 

However, the above m_bselectall is used to determine whether the "All selected" status has a problem, that is, when all is selected, the user clicks the edit box to cancel all selection, but at this time, m_bselectall is still in the all-selected state, that is, this sign (m_bselectall) is inconsistent with the actual all-selected state. Therefore, another method is used to verify whether the selected content is in the all-selected status (that is, whether the length of the selected content is consistent with the content length of the edit box). The Code is as follows:

Bool cd1dlg: pretranslatemessage (MSG * PMSG) {If (PMSG-> hwnd = m_pedit-> m_hwnd & (getasynckeystate (vk_control) & 0x8000) & (getasynckeystate (_ T ('A') & 0x8000) {cstring txt; int start, end; m_pedit-> getwindowtext (txt ); m_pedit-> getsel (START, end); If (txt. getlength () = end-Start) // In the all-selected status {m_pedit-> setsel (-1); // cancel all-selected} else {m_pedit-> setsel (0, -1); // select all} return true;} return cdialog: pretranslatemessage (PMSG );}

 

 

Description: Getasynckeystate () only checks whether a key is pressed, not case sensitive!

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.