Solved

Remove selection from initialized text (Deselect) in CEdit control

Posted on 2004-09-18
8
2,283 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
 
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Show shut-down message as Windows 8 shuts down. 9 74
string initialization in java 11 103
sum28 challenge 31 96
FizzBuzz challenge 9 73
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.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now