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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. CEdit SetSel and Scrolling

CEdit SetSel and Scrolling

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
6 Posts 2 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.
  • T Offline
    T Offline
    Thomas Blenkers
    wrote on last edited by
    #1

    Hello everyone, this is a sounds-so-simple gray hair maker. Please help out: A single line edit control on my dialog contains So far nothing complicated. Now: On an event I would like to add some long text at the cursor position, give the edit box the focus, select the whole text of the edit control ***AND** I would like to see the beginning of my long selected text ... CEdit *pEdit = (CEdit *)GetDlgItem(IDC_EDIT1); pEdit->ReplaceSel("This is some long and rather useless test text"); pEdit->SetSel(0, -1); pEdit->SetFocus(); The problem is that the SetSel scrolls the edit box to the end of the selection. Is there a way to scroll to the beginning without loosing the selection? If you know the length of the selection, you may do both .SetSel(Start, End) or .SetSel(End, Start) but always the caret and the scroll will be at the end of the selection. WM_HELP Thomas

    N 1 Reply Last reply
    0
    • T Thomas Blenkers

      Hello everyone, this is a sounds-so-simple gray hair maker. Please help out: A single line edit control on my dialog contains So far nothing complicated. Now: On an event I would like to add some long text at the cursor position, give the edit box the focus, select the whole text of the edit control ***AND** I would like to see the beginning of my long selected text ... CEdit *pEdit = (CEdit *)GetDlgItem(IDC_EDIT1); pEdit->ReplaceSel("This is some long and rather useless test text"); pEdit->SetSel(0, -1); pEdit->SetFocus(); The problem is that the SetSel scrolls the edit box to the end of the selection. Is there a way to scroll to the beginning without loosing the selection? If you know the length of the selection, you may do both .SetSel(Start, End) or .SetSel(End, Start) but always the caret and the scroll will be at the end of the selection. WM_HELP Thomas

      N Offline
      N Offline
      Nibu babu thomas
      wrote on last edited by
      #2

      Thomas Blenkers wrote:

      pEdit->SetSel(0, -1); pEdit->SetFocus();

      SetSel has one more parameter which is a default one. Does that help you? Look up the docs.


      Nibu thomas Software Developer Faqs by Michael dunn

      T 1 Reply Last reply
      0
      • N Nibu babu thomas

        Thomas Blenkers wrote:

        pEdit->SetSel(0, -1); pEdit->SetFocus();

        SetSel has one more parameter which is a default one. Does that help you? Look up the docs.


        Nibu thomas Software Developer Faqs by Michael dunn

        T Offline
        T Offline
        Thomas Blenkers
        wrote on last edited by
        #3

        Thanks, Nibu, yes I do read docs before asking ;) The third parameter is called bNoScroll and according to the docs it should scroll or not the caret into view. But this is not the point, since I tried both versions. Both get the same result: the edit box is completely selected with the end portion of the text being shown. My guess is that the problem lies in the line of code before the SetSel: CEdit *pEdit = (CEdit *)GetDlgItem(IDC_EDIT1); pEdit->ReplaceSel("This is some long and rather useless test text"); pEdit->SetSel(0, -1); pEdit->SetFocus(); The ReplaceSel will put the caret to the end of the edit windows and scrolls the text to the end. Already I have played around with the edit box scrolling functions to no avail. As I said, its making you gray hairs! Regards Thomas

        N 1 Reply Last reply
        0
        • T Thomas Blenkers

          Thanks, Nibu, yes I do read docs before asking ;) The third parameter is called bNoScroll and according to the docs it should scroll or not the caret into view. But this is not the point, since I tried both versions. Both get the same result: the edit box is completely selected with the end portion of the text being shown. My guess is that the problem lies in the line of code before the SetSel: CEdit *pEdit = (CEdit *)GetDlgItem(IDC_EDIT1); pEdit->ReplaceSel("This is some long and rather useless test text"); pEdit->SetSel(0, -1); pEdit->SetFocus(); The ReplaceSel will put the caret to the end of the edit windows and scrolls the text to the end. Already I have played around with the edit box scrolling functions to no avail. As I said, its making you gray hairs! Regards Thomas

          N Offline
          N Offline
          Nibu babu thomas
          wrote on last edited by
          #4

          Thomas Blenkers wrote:

          CEdit *pEdit = (CEdit *)GetDlgItem(IDC_EDIT1); pEdit->ReplaceSel("This is some long and rather useless test text"); pEdit->SetSel(0, -1); pEdit->SetFocus();

          CEdit *pEdit = (CEdit *)GetDlgItem(IDC_EDIT1);
          pEdit->SetSel(0, -1);
          pEdit->ReplaceSel("This is some long and rather useless test text");
          CString csText;
          pEdit->GetWindowText(csText);
          pEdit->SetWindowText(csText);

          pEdit->SetFocus();

          This should help although it doesn't look good. :) Unless another suitable option is found you can use this. :)


          Nibu thomas Software Developer Faqs by Michael dunn

          T 1 Reply Last reply
          0
          • N Nibu babu thomas

            Thomas Blenkers wrote:

            CEdit *pEdit = (CEdit *)GetDlgItem(IDC_EDIT1); pEdit->ReplaceSel("This is some long and rather useless test text"); pEdit->SetSel(0, -1); pEdit->SetFocus();

            CEdit *pEdit = (CEdit *)GetDlgItem(IDC_EDIT1);
            pEdit->SetSel(0, -1);
            pEdit->ReplaceSel("This is some long and rather useless test text");
            CString csText;
            pEdit->GetWindowText(csText);
            pEdit->SetWindowText(csText);

            pEdit->SetFocus();

            This should help although it doesn't look good. :) Unless another suitable option is found you can use this. :)


            Nibu thomas Software Developer Faqs by Michael dunn

            T Offline
            T Offline
            Thomas Blenkers
            wrote on last edited by
            #5

            Nibu, once again, thanks and great work. This will work as long as indeed I choose to select all of the edit box. In my real application I'm doing some autocompletion so I want a first part of the edit text not selected while my recenty added text is selected. Maybe its my fault that my given lines of code were too much siplified to express this contraint. If you have further ideas, I would be really happy. Regards Thomas

            N 1 Reply Last reply
            0
            • T Thomas Blenkers

              Nibu, once again, thanks and great work. This will work as long as indeed I choose to select all of the edit box. In my real application I'm doing some autocompletion so I want a first part of the edit text not selected while my recenty added text is selected. Maybe its my fault that my given lines of code were too much siplified to express this contraint. If you have further ideas, I would be really happy. Regards Thomas

              N Offline
              N Offline
              Nibu babu thomas
              wrote on last edited by
              #6

              Thomas Blenkers wrote:

              In my real application I'm doing some autocompletion so I want a first part of the edit text not selected while my recenty added text is selected.

              So no problem. You first do whatever replacement or other stuff that you wish to do. Then do GetWindowText and SetWindowText. I know that's not a natural solution. But here are some other functions that I would like to bring to your notice... SetCaretPos --> CWnd PosFromChar --> CEdit I tried these two but the caret didn't move. But you can try. Maybe you can make it work. :)


              Nibu thomas Software Developer Programming Tips[^]

              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