Solved

Remove selection from initialized text (Deselect) in CEdit control

Posted on 2004-09-18
8
2,372 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

733 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