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. C / C++ / MFC
  3. Problems scrolling in an edit box

Problems scrolling in an edit box

Scheduled Pinned Locked Moved C / C++ / MFC
8 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.
  • F Offline
    F Offline
    Fredrik Skog
    wrote on last edited by
    #1

    Hi, Has anyone any ideas why LineScroll() does not work in a subclassed edit box? When inserting text and trying to scroll to the end, a carriage return is added to the top of the text and the caret goes to the second line. It looks pretty much like this: CString Container(""); int nLines; GetWindowText(Container); Container += "\r\n" + strText; SetWindowText(Container); nLines = GetLineCount(); LineScroll(nLines); Cheers, /Fredrik Do you Sonork? I do! 100.11430 PhatBoy

    J 1 Reply Last reply
    0
    • F Fredrik Skog

      Hi, Has anyone any ideas why LineScroll() does not work in a subclassed edit box? When inserting text and trying to scroll to the end, a carriage return is added to the top of the text and the caret goes to the second line. It looks pretty much like this: CString Container(""); int nLines; GetWindowText(Container); Container += "\r\n" + strText; SetWindowText(Container); nLines = GetLineCount(); LineScroll(nLines); Cheers, /Fredrik Do you Sonork? I do! 100.11430 PhatBoy

      J Offline
      J Offline
      James R Twine
      wrote on last edited by
      #2

      Hmmm...  Will LineScroll(...) work of you do not have a scrollbar visible?  If you are trying to "get to the end" of the text, why not use SetSel(...) to move the caret to the end of the text?    Or am I just missing what you are trying to do?    BTW: try not to use "" to represent an empty string with dealing with CStrings, use afxEmptyString instead.  And, you do not need to initialize a CString with an empty string.  It constructs to an empty string by default.  (Initializing it with an empty string just wastes time.) -=- James.

      F 1 Reply Last reply
      0
      • J James R Twine

        Hmmm...  Will LineScroll(...) work of you do not have a scrollbar visible?  If you are trying to "get to the end" of the text, why not use SetSel(...) to move the caret to the end of the text?    Or am I just missing what you are trying to do?    BTW: try not to use "" to represent an empty string with dealing with CStrings, use afxEmptyString instead.  And, you do not need to initialize a CString with an empty string.  It constructs to an empty string by default.  (Initializing it with an empty string just wastes time.) -=- James.

        F Offline
        F Offline
        Fredrik Skog
        wrote on last edited by
        #3

        Well, the scrollbar is visible, and I am indeed trying to scroll to the last line of the box. I tried to use SetSel(), but then the text disappears... Using LineScroll() works with a non-subclassed edit box. I have written handlers for OnChar(), OnKeyDown() and OnRButtonDown() in the subclassed version. Could that cause any trouble? Cheers, /Fredrik Do you Sonork? I do! 100.11430 PhatBoy

        J 1 Reply Last reply
        0
        • F Fredrik Skog

          Well, the scrollbar is visible, and I am indeed trying to scroll to the last line of the box. I tried to use SetSel(), but then the text disappears... Using LineScroll() works with a non-subclassed edit box. I have written handlers for OnChar(), OnKeyDown() and OnRButtonDown() in the subclassed version. Could that cause any trouble? Cheers, /Fredrik Do you Sonork? I do! 100.11430 PhatBoy

          J Offline
          J Offline
          James R Twine
          wrote on last edited by
          #4

          > I have written handlers for OnChar(), OnKeyDown() and > OnRButtonDown() in the subclassed version. Could that > cause any trouble?    I would not think so.  And what do you mean by "the text dissa[ears" when you use SetSel(...)? -=- James.

          F 1 Reply Last reply
          0
          • J James R Twine

            > I have written handlers for OnChar(), OnKeyDown() and > OnRButtonDown() in the subclassed version. Could that > cause any trouble?    I would not think so.  And what do you mean by "the text dissa[ears" when you use SetSel(...)? -=- James.

            F Offline
            F Offline
            Fredrik Skog
            wrote on last edited by
            #5

            ****James R. Twine wrote: I would not think so. And what do you mean by "the text disappears" when you use SetSel(...)? The text that is entered in the edit box disappears. It is not visible, and it can't be retrieved with GetWindowText(). :confused: Cheers, /Fredrik Do you Sonork? I do! 100.11430 PhatBoy

            F 1 Reply Last reply
            0
            • F Fredrik Skog

              ****James R. Twine wrote: I would not think so. And what do you mean by "the text disappears" when you use SetSel(...)? The text that is entered in the edit box disappears. It is not visible, and it can't be retrieved with GetWindowText(). :confused: Cheers, /Fredrik Do you Sonork? I do! 100.11430 PhatBoy

              F Offline
              F Offline
              Fredrik Skog
              wrote on last edited by
              #6

              I realized if have been a bit unclear :-O ; what I am doing is that I have subclassed an edit box and added an OnChar handler. The OnChar handler adds text and scrolls to the bottom. Looks something like this: void CMySubclassedEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) {     CString strText("Text\r\n>");     CString Container;     int nLines;     if (nChar == 0x0D)     {         GetWindowText(Container);         Container += "\r\n" + strText;         SetWindowText(Container);         nLines = GetLineCount();         LineScroll(nLines);     }     CEdit::OnChar(nChar, nRepCnt, nFlags); } Cheers, /Fredrik Do you Sonork? I do! 100.11430 PhatBoy

              J 1 Reply Last reply
              0
              • F Fredrik Skog

                I realized if have been a bit unclear :-O ; what I am doing is that I have subclassed an edit box and added an OnChar handler. The OnChar handler adds text and scrolls to the bottom. Looks something like this: void CMySubclassedEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) {     CString strText("Text\r\n>");     CString Container;     int nLines;     if (nChar == 0x0D)     {         GetWindowText(Container);         Container += "\r\n" + strText;         SetWindowText(Container);         nLines = GetLineCount();         LineScroll(nLines);     }     CEdit::OnChar(nChar, nRepCnt, nFlags); } Cheers, /Fredrik Do you Sonork? I do! 100.11430 PhatBoy

                J Offline
                J Offline
                James R Twine
                wrote on last edited by
                #7

                >> The text that is entered in the edit box disappears. >> It is not visible, and it can't be retrieved with GetWindowText().    If you are using the above code, and the text dissapears when you use SetSel(...), it might be due to the fact that your call to SetSel is incorrect, and is selecting the entire control.  Then, the base CEdit::OnChar(...) gets called, which will replace the selection with whatever character was entered (a CR or LF); the result: no text in the control.    Also, in the above code, "nLines" is not initialized.  That might result in some strange scrolling effects.    Lastly, if all you want to do is add a CRLF and some text to an edit control, the easiest (and faster than the code you posted) is to do a CEdit::SetSel(...) to get to the end of the control, and then call CEdit::ReplaceSel(...) with the text you want to add to the control.  And since pasting normally moves the caret to the end of the pastes text, you will still be at the end of the edit control's text.    Peace! -=- James.

                F 1 Reply Last reply
                0
                • J James R Twine

                  >> The text that is entered in the edit box disappears. >> It is not visible, and it can't be retrieved with GetWindowText().    If you are using the above code, and the text dissapears when you use SetSel(...), it might be due to the fact that your call to SetSel is incorrect, and is selecting the entire control.  Then, the base CEdit::OnChar(...) gets called, which will replace the selection with whatever character was entered (a CR or LF); the result: no text in the control.    Also, in the above code, "nLines" is not initialized.  That might result in some strange scrolling effects.    Lastly, if all you want to do is add a CRLF and some text to an edit control, the easiest (and faster than the code you posted) is to do a CEdit::SetSel(...) to get to the end of the control, and then call CEdit::ReplaceSel(...) with the text you want to add to the control.  And since pasting normally moves the caret to the end of the pastes text, you will still be at the end of the edit control's text.    Peace! -=- James.

                  F Offline
                  F Offline
                  Fredrik Skog
                  wrote on last edited by
                  #8

                  ****James R. Twine wrote: >If you are using the above code, and the text dissapears when you use SetSel(...),
                  >it might be due to the fact that your call to SetSel is incorrect, and is selecting the entire control.
                  >Then, the base CEdit::OnChar(...) gets called,
                  >which will replace the selection with whatever character was entered (a CR or LF);
                  >the result: no text in the control.

                  Ah, right you are. It works if I remove the selection afterwards, naturally. Thank you for the help, much appreciated! Cheers,
                  /Fredrik

                  Do you Sonork? I do! 100.11430:PhatBoy

                  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