Question

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

Was it helpful?

Solution

I don't think that such a style exists.
But you can add OnSetfocus handler with the wizard:

void CMyDlg::OnSetfocusEdit1() 
{
  CEdit* e = (CEdit*)GetDlgItem(IDC_EDIT1);
  e->SetSel(0); // <-- hide selection
}

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top