Select text inside edit box
-
Hi, this is my problem. I have an MFC diaog based application with multiple edit boxes. What I need to do is to automatically select all the text inside a box when clicking on it. That's just what happens on Explorer address bar... I've tried writing inside each OnSetfocusEdit function m_edit.SetSel(0,-1); m_edit.SetFocus(); I also tried to write only the line m_edit.SetSel(0,-1); but it didn't work. If I put the same 2 lines in a button click the operation succeeds. Why is this? Can anyone help me? Thank you in advance,:) Marco.
-
Hi, this is my problem. I have an MFC diaog based application with multiple edit boxes. What I need to do is to automatically select all the text inside a box when clicking on it. That's just what happens on Explorer address bar... I've tried writing inside each OnSetfocusEdit function m_edit.SetSel(0,-1); m_edit.SetFocus(); I also tried to write only the line m_edit.SetSel(0,-1); but it didn't work. If I put the same 2 lines in a button click the operation succeeds. Why is this? Can anyone help me? Thank you in advance,:) Marco.
-
Hi, this is my problem. I have an MFC diaog based application with multiple edit boxes. What I need to do is to automatically select all the text inside a box when clicking on it. That's just what happens on Explorer address bar... I've tried writing inside each OnSetfocusEdit function m_edit.SetSel(0,-1); m_edit.SetFocus(); I also tried to write only the line m_edit.SetSel(0,-1); but it didn't work. If I put the same 2 lines in a button click the operation succeeds. Why is this? Can anyone help me? Thank you in advance,:) Marco.
If the clicking of an edit control selects all of the text contained within it, how is a person able to make a correction? Normally, a double-click of an edit control selects all of the text contained within it, and a single-click merely places the edit cursor at some point within the control. Doesn't this sound counterintuitive?
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
-
the SetFocus call is not necessary anymore in the OnSetFocus.. functions, since it allready is getting the focus. That`s probably the source of all evil... Regards, Davy
-
Hi Marco, Try the PreTranslateMessage override, e.g. for an IE address bar type behaviour...
BOOL CMyDlg::PreTranslateMessage(MSG* pMsg)
{
BOOL bEatMe = FALSE;if (pMsg && pMsg->message == WM\_LBUTTONDOWN) { CEdit\* pEdit = (CEdit\*)GetDlgItem(IDC\_EDIT1); // Is this our edit control? if (pEdit && pEdit->GetSafeHwnd() && pMsg->hwnd == pEdit->GetSafeHwnd()) { // select CWnd\* pPrevFocus = pEdit->SetFocus(); if (pPrevFocus == pEdit) { // already got focus so normal operation } else { // first time focus, so select all bEatMe = TRUE; pEdit->SetSel(0,-1, TRUE); } } } // return if we've processed this message or want the default/system to return (bEatMe) ? TRUE:CDialog::PreTranslateMessage(pMsg);
}
Hope this helps, Andy
-
If the clicking of an edit control selects all of the text contained within it, how is a person able to make a correction? Normally, a double-click of an edit control selects all of the text contained within it, and a single-click merely places the edit cursor at some point within the control. Doesn't this sound counterintuitive?
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
I think you'd want this behavior only if the user is likely to almost always replace the existing text. The user can still modify the text by pressing "End", "Home" or the arrow keys to first clear the selection, although this involves an extra keystroke. /ravi My new year's resolution: 2048 x 1536 Home | Articles | Freeware | Music ravib@ravib.com
-
Hi Marco, Try the PreTranslateMessage override, e.g. for an IE address bar type behaviour...
BOOL CMyDlg::PreTranslateMessage(MSG* pMsg)
{
BOOL bEatMe = FALSE;if (pMsg && pMsg->message == WM\_LBUTTONDOWN) { CEdit\* pEdit = (CEdit\*)GetDlgItem(IDC\_EDIT1); // Is this our edit control? if (pEdit && pEdit->GetSafeHwnd() && pMsg->hwnd == pEdit->GetSafeHwnd()) { // select CWnd\* pPrevFocus = pEdit->SetFocus(); if (pPrevFocus == pEdit) { // already got focus so normal operation } else { // first time focus, so select all bEatMe = TRUE; pEdit->SetSel(0,-1, TRUE); } } } // return if we've processed this message or want the default/system to return (bEatMe) ? TRUE:CDialog::PreTranslateMessage(pMsg);
}
Hope this helps, Andy