Link to home
Start Free TrialLog in
Avatar of George Yar
George Yar

asked on

Default text selection in CEdit box.

This question is for Visual C++ MFC application in Visual Studio Community 2015 under Windiws 7.

When you set some text in multi line CEdit box in OnInitDialog function, that is called to initialize dialog's controls. the text is displayed as selected (highlighted.) No manipulations with CEdit interface in OnInitDialog can't remove this highlighting (Like using SetSel(-1,0).)  
Somebody knows how to initialize CEdit with no selection?
Avatar of Zoppo
Zoppo
Flag of Germany image

Hi George Yar,

it seems it's the default behavior for edit controls, whenever they recieve the input focus the complete content is selected.

The easiest way to workaround this is to override a EN_SETFOCUS notification message handler in the dialog and call the SetSel there. You ocan do this like this:
- select the edit control in resource editor
- in 'Proprties' tool window click the 'Control Events' toolbar button
- Search 'EN_SETFOUCS' and select '<Add new>'

Then add the SetSel-call, i.e.:
void CMFCApplication1Dlg::OnEnSetfocusEditMulti()
{
	m_wndEditMulti.SetSel( -1, 0 );
}

Open in new window

This should work, but IMO it's not the best solution since changing focus to another control and back to the edit control alway will do the same without remembering manual selection change. Probably that's ok for, but if not (iow. if you want the edit control to remember its selection) please tell, then I can try to provide a better solution using a CEdit-derived class.

Hope that helps,

ZOPPO
Avatar of George Yar
George Yar

ASKER

Yes, it is by default. To disable selection in read-only CEdit it is enough to disable tab stop in VS resource editor for this CEdit.
Also, if the edit control is on Property page of property Sheet, you have to use virtual function OnAvtivate of CPropertySheet and DYNAMIC_DOWNCAST with SetSel(-1, -1)  for to get right control.
All these solutions are not ideal, but I continue to look for something better
IMO disabling tabstop is not a good idea - for convenience I wrote a small CEdit-derived class which stores selection when focus is lost and restores it when focus is set again. To use it just add the two attached files, include the header in the dialog class header and subclass the wanted edit controls with CPreSelEdit (I guess you know how such subclassing is done easiest? Just add a member variabel for the edit control of type control/CEdit via ClassWizard, then manually replace the CEdit in the header with CPreSelEdit) to use it.

Hope that helps,

ZOPPO
PreSelEdit.h
PreSelEdit.cpp
May be I was not clear enough, but my problem was that an edit control went visible with all text programmatically written into it selected (highlighted.) The control had tab stop #1 and OnInitDialog automatically was setting focus to it. When focus goes to other control, selection goes out, and does not return if focus returns. So you need to act only when an edit control gets focus upon app returns from OnInitDialog or OnActivate.
Subclassing definitely helps because there are some additional effects on multi-line CEdit with first tab stop.
anyway, thank you.
Hm - ok, so the only thing you want to change from the default behavior is that after OnInitDialog all text in the textbox, which has the focus, is selected? Without changing the default behavior when the focus is changed, so all text is shown selected again when the edit box gets focused again i.e. with TAB?

If so it should be enough to set the focus and change the selection in OnInitDialog and change the return value:
BOOL CMFCApplication1Dlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	//...

	m_wndEditMulti.SetFocus();

	// here I set the cursor to the end of the text, if you don't do this it will be set
	//  to the beginning. If you want this just remove these two lines
	int len = m_wndEditMulti.GetWindowTextLength();
	m_wndEditMulti.SetSel( len, len );

	return FALSE;  // return TRUE  unless you set the focus to a control
}

Open in new window

Ensure you return FALSE instead of TRUE, otherwise the base-class functionality itself calls SetFocus which again sets the selection.

Best regards,

ZOPPO
Thank you for reply.

I tried it before and it did not work, I did not tried to set focus to OK or CANCEL buttons.
Besides, what if the dialog box has only one control?

This is what I have observed on CMFCPropertyPage with three read-only multi-line CEdit:
1.In CEdit with the first tab stop all text is selected.
2.Any mouse click on this control or other controls remove the selection but moves vertical scroll position to the end of the text.
3. After switch to other page and return back the text is highlighted again.

In my opinion the disabling  of tab stops for edit control is not a such big loss.
And all actions should be done in OnActivate function.
Of course subclassing also can  help.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.