Link to home
Create AccountLog in
System Programming

System Programming

--

Questions

--

Followers

Top Experts

Avatar of Alkali_Guy
Alkali_Guy

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?

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


ASKER CERTIFIED SOLUTION
Avatar of Jaime OlivaresJaime Olivares🇵🇪

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

Avatar of Jaime OlivaresJaime Olivares🇵🇪

Also maybe you will need an initial "deselection" at the end of OnInitDialog event.

Avatar of Alkali_GuyAlkali_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.


Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


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.

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


I discovered the malfunction.  I use SetWindowPos() in OnInitDialog() to resize it, which requires a change so that OnInitDialog() returns FALSE.

:-S
System Programming

System Programming

--

Questions

--

Followers

Top Experts

Kernel and system programming is the process of creating the software necessary for a computer or device to function and operate other programs. Some operating systems (such as Microsoft Windows) are proprietary, but others, such as the various Linux distributions, are open source.