Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Remove selection from initialized text (Deselect) in CEdit control

Posted on 2004-09-18
8
Medium Priority
?
2,535 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 500 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month12 days, 19 hours left to enroll

972 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