Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CEdit selects everything when getting focus

When I move to a CEdit control on my dialog using the tab key or the arrow keys all the text in the control is selected. This behaviour is causing me problems and I would prefer it if the control just put the cursor at the start (or end) of the text and didn't select anything. Is there a simple way to do this (e.g. a property of the control that I can set)?

like image 474
Hoppy Avatar asked Jan 02 '10 18:01

Hoppy


1 Answers

Another way of achieving your goal is to prevent the contents from being selected. When navigating over controls in a dialog the dialog manager queries the respective controls about certain properties pertaining to their behavior. By default an edit control responds with a DLGC_HASSETSEL flag (among others) to indicate to the dialog manager that its contents should be auto-selected.

To work around this you would have to subclass the edit control and handle the WM_GETDLGCODE message to alter the flags appropriately. First, derive a class from CEdit:

class CPersistentSelectionEdit : public CEdit {
public:
    DECLARE_MESSAGE_MAP()
    afx_msg UINT OnGetDlgCode() {
        // Return default value, removing the DLGC_HASSETSEL flag
        return ( CEdit::OnGetDlgCode() & ~DLGC_HASSETSEL );
    }
};

BEGIN_MESSAGE_MAP( CPersistentSelectionEdit, CEdit )
    ON_WM_GETDLGCODE()
END_MESSAGE_MAP()

Next subclass the actual control. There are a number of ways to do this. To keep things simple just declare a class member m_Edit1 of type CPersistentSelectionEdit in your dialog class and add an appropriate entry in DoDataExchange:

// Subclass the edit control
DDX_Control( pDX, IDC_EDIT1, m_Edit1 );

At this point you have an edit control that doesn't have its contents auto-selected when navigated to. You can control the selection whichever way you want.

like image 115
IInspectable Avatar answered Sep 27 '22 20:09

IInspectable