Link to home
Start Free TrialLog in
Avatar of Alkali_Guy
Alkali_Guy

asked on

Remove selection from initialized text (Deselect) in CEdit control

I am initializing a CEdit control in MFC with a string using DDX.  Normally, this string is selected when the dialog box opens.  I need to stop this behavior and have no selection.  Usually this is easy:

m_myEdit.SetSel(-1, 0, false);

However, initialization seems to be a special case.  I've tried placing the above line in CDialog::DoDataExchange, CDialog::OnInitDialog, and CEdit::OnSetfocus without any success.

I've also tried subclassing CEdit as "CSelEdit" using this in OnInitDialog()...:
m_mySelEdit.SubclassDlgItem(IDC_MYEDIT, this);
..and the ON_CONTROL_REFLECT macro in the CSelEdit class.

Can you recommend a modification of one of these methods, or an entirely new approach, in order to simply deselect initialized text in a CEdit control?
ASKER CERTIFIED SOLUTION
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Also maybe you will need an initial "deselection" at the end of OnInitDialog event.
Avatar of Alkali_Guy
Alkali_Guy

ASKER

Jaime: Thanks for the suggestion, but although SetSel(...) is executed, the text ends up selected anyway.

Note that if you tab through the controls back to ID_EDIT_1 again, the selection goes away properly.  It's only on initialization that it fails.  I don't know if this matters, but the DDX member is a CString rather than a CEdit object for the text box.

Maybe a SetFocus() is needed?  Or maybe there's a way to intercept any EM_SETSEL message that could be causing the problem.
I have implemented my solutions and works ok, with a CString member. As I told you, the selection is made at the focus event, it is not related to the CString.
When dialog is initialized, the focus is set to the first control in "tab order" list. At this point, the focus event is "trapped" by your event handler and selection is cancelled.

Does anything need to be done in OnInitDialog() for this to work?
[In the forum on this page there is a comment about a similar problem (although it's for PocketPC):]

http://www.codeproject.com/editctrl/editctrltutorial.asp?df=100&forumid=1672&select=752761#xxxx
>Does anything need to be done in OnInitDialog() for this to work?
I think no. I have tried.
I discovered the malfunction.  I use SetWindowPos() in OnInitDialog() to resize it, which requires a change so that OnInitDialog() returns FALSE.

:-S