Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Select text inside edit box

Select text inside edit box

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++question
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    marcomars
    wrote on last edited by
    #1

    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.

    G D 2 Replies Last reply
    0
    • M marcomars

      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.

      G Offline
      G Offline
      GDavy
      wrote on last edited by
      #2

      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

      M 1 Reply Last reply
      0
      • M marcomars

        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.

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #3

        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

        R 1 Reply Last reply
        0
        • G GDavy

          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

          M Offline
          M Offline
          marcomars
          wrote on last edited by
          #4

          Already done, but it doesn't work... Any other suggestion? Thanks, Marco.

          A 1 Reply Last reply
          0
          • M marcomars

            Already done, but it doesn't work... Any other suggestion? Thanks, Marco.

            A Offline
            A Offline
            Andrew Quinn AUS
            wrote on last edited by
            #5

            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

            M 1 Reply Last reply
            0
            • D David Crow

              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

              R Offline
              R Offline
              Ravi Bhavnani
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              • A Andrew Quinn AUS

                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

                M Offline
                M Offline
                marcomars
                wrote on last edited by
                #7

                ;);););););););););););););););) Thanks a lot !!!!!! Now it WORKS !!!!!! Marco. ;);););););););););););););););)

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups