Solved

Remove selection from initialized text (Deselect) in CEdit control

Posted on 2004-09-18
8
2,351 Views
Last Modified: 2013-11-20
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?
0
Comment
Question by:Alkali_Guy
  • 4
  • 4
8 Comments
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 125 total points
ID: 12092954
The problem is in the Edit's focus event.
You can make a test:
Go to ClassWizard, select your dialog class, and select the Edit item, let say ID_EDIT_1.
Now select the EN_SETFOCUS notify and create an event handler for it, let say OnSetfocusEdit1.
Now implement the event:

void CYourDlg::OnSetfocusEdit1()
{
       ((CEdit *)GetDlgItem(ID_EDIT_1))->SetSel(-1, 0, false);
}
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12092960
Also maybe you will need an initial "deselection" at the end of OnInitDialog event.
0
 
LVL 5

Author Comment

by:Alkali_Guy
ID: 12093166
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.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12093216
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.

0
 
LVL 5

Author Comment

by:Alkali_Guy
ID: 12093302
Does anything need to be done in OnInitDialog() for this to work?
0
 
LVL 5

Author Comment

by:Alkali_Guy
ID: 12093311
[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
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12093329
>Does anything need to be done in OnInitDialog() for this to work?
I think no. I have tried.
0
 
LVL 5

Author Comment

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

:-S
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question