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#
  4. Scrollbar problem

Scrollbar problem

Scheduled Pinned Locked Moved C#
questionhelp
15 Posts 3 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.
  • A Offline
    A Offline
    andreas_farnstrand
    wrote on last edited by
    #1

    Hi! I have a richtextbox with a vertical scrollbar. My problem is that the scrollbar is always at the top. How do I get the scrollbar to always be down, showing the lastest added text? Andreas Färnstrand

    D L 3 Replies Last reply
    0
    • A andreas_farnstrand

      Hi! I have a richtextbox with a vertical scrollbar. My problem is that the scrollbar is always at the top. How do I get the scrollbar to always be down, showing the lastest added text? Andreas Färnstrand

      D Offline
      D Offline
      Dennis C Dietrich
      wrote on last edited by
      #2

      Hi Andreas! I have a richtextbox with a vertical scrollbar. My problem is that the scrollbar is always at the top. You can use the TextBoxBase.ScrollToCaret[^] method.

      richTextBox1.SelectionStart = richTextBox1.Text.Length;
      richTextBox1.SelectionLength = 0;
      richTextBox1.ScrollToCaret();

      Best regards Dennis

      A 1 Reply Last reply
      0
      • A andreas_farnstrand

        Hi! I have a richtextbox with a vertical scrollbar. My problem is that the scrollbar is always at the top. How do I get the scrollbar to always be down, showing the lastest added text? Andreas Färnstrand

        D Offline
        D Offline
        Dennis C Dietrich
        wrote on last edited by
        #3

        Just a little supplement: if you're using the .NET Framework 2.0 you will encounter an error in the Beta 1 Build of the RichTextBox (see RichTextBox ScrollToCaret has different behavior then TextBox ScrollToCaret[^]) Best regards Dennis

        1 Reply Last reply
        0
        • D Dennis C Dietrich

          Hi Andreas! I have a richtextbox with a vertical scrollbar. My problem is that the scrollbar is always at the top. You can use the TextBoxBase.ScrollToCaret[^] method.

          richTextBox1.SelectionStart = richTextBox1.Text.Length;
          richTextBox1.SelectionLength = 0;
          richTextBox1.ScrollToCaret();

          Best regards Dennis

          A Offline
          A Offline
          andreas_farnstrand
          wrote on last edited by
          #4

          Hi Dennis! Thanks for your help. It doesn't seem to work though. Here's my code: private void Print(string text){ this.messagesrichtextbox.AppendText(text + "\n"); this.messagesrichtextbox.SelectionStart = this.messagesrichtextbox.Text.Length; this.messagesrichtextbox.SelectionLength = 0; this.messagesrichtextbox.ScrollToCaret(); } Simple enough, but it won't work. I've tried adding a this.messagesrichtextbox.Select also but it didn't work either. What's wrong? Thanx, Andreas Andreas Färnstrand

          D 1 Reply Last reply
          0
          • A andreas_farnstrand

            Hi Dennis! Thanks for your help. It doesn't seem to work though. Here's my code: private void Print(string text){ this.messagesrichtextbox.AppendText(text + "\n"); this.messagesrichtextbox.SelectionStart = this.messagesrichtextbox.Text.Length; this.messagesrichtextbox.SelectionLength = 0; this.messagesrichtextbox.ScrollToCaret(); } Simple enough, but it won't work. I've tried adding a this.messagesrichtextbox.Select also but it didn't work either. What's wrong? Thanx, Andreas Andreas Färnstrand

            D Offline
            D Offline
            Dennis C Dietrich
            wrote on last edited by
            #5

            Hi Andreas! Thanks for your help. It doesn't seem to work though. Well, I've tested this using VS 2005 so this difference in the behaviour could be due to chances made to the .NET Framework 2.0. Try setting the focus to the RichTextBox before calling ScrollToCaret (right now I don't have the Framework 1.1 installed so I can't verfiy if that's the problem). If this solves your problem you might want to add a few lines to restore the focus after scrolling the text.

            richTextBox1.SelectionStart = richTextBox1.Text.Length;
            richTextBox1.SelectionLength = 0;
            richTextBox1.Focus();
            richTextBox1.ScrollToCaret();

            Best regards Dennis

            A 1 Reply Last reply
            0
            • D Dennis C Dietrich

              Hi Andreas! Thanks for your help. It doesn't seem to work though. Well, I've tested this using VS 2005 so this difference in the behaviour could be due to chances made to the .NET Framework 2.0. Try setting the focus to the RichTextBox before calling ScrollToCaret (right now I don't have the Framework 1.1 installed so I can't verfiy if that's the problem). If this solves your problem you might want to add a few lines to restore the focus after scrolling the text.

              richTextBox1.SelectionStart = richTextBox1.Text.Length;
              richTextBox1.SelectionLength = 0;
              richTextBox1.Focus();
              richTextBox1.ScrollToCaret();

              Best regards Dennis

              A Offline
              A Offline
              andreas_farnstrand
              wrote on last edited by
              #6

              Hi again! I'm sorry! It does work. The problem seems to be that there's a loop that wont let SetToCaret() work until it's finished. You can add text to the richtextbo but it doesn't scroll down until I shut the loop down. The loop is lístening to a Socket, while( online ). Thanks again, Andreas Andreas Färnstrand

              D 1 Reply Last reply
              0
              • A andreas_farnstrand

                Hi again! I'm sorry! It does work. The problem seems to be that there's a loop that wont let SetToCaret() work until it's finished. You can add text to the richtextbo but it doesn't scroll down until I shut the loop down. The loop is lístening to a Socket, while( online ). Thanks again, Andreas Andreas Färnstrand

                D Offline
                D Offline
                Dennis C Dietrich
                wrote on last edited by
                #7

                Hi Andreas! The problem seems to be that there's a loop that wont let SetToCaret() work until it's finished. Then try to call RichTextBox.Refresh() after RichTextBox.ScrollToCaret(). The loop is lístening to a Socket, while( online ). Another idea would be to use asynchronous calls to get the data (see Using an Asynchronous Client Socket[^]). Best regards Dennis

                1 Reply Last reply
                0
                • A andreas_farnstrand

                  Hi! I have a richtextbox with a vertical scrollbar. My problem is that the scrollbar is always at the top. How do I get the scrollbar to always be down, showing the lastest added text? Andreas Färnstrand

                  L Offline
                  L Offline
                  leppie
                  wrote on last edited by
                  #8

                  This has been asked 10 gazillion times, so here goes one solution:

                  public class RichTextBox : System.Windows.Forms.RichTextBox
                  {
                  void ScrollToEnd()
                  {
                  const int msg = 0x400 + 222;
                  int h = Height;
                  int y = Font.Height * Lines.Length - h;
                  Point point = new Point(0, y);
                  SendMessage(Handle, msg, 0, ref point);
                  }

                  [DllImport("user32.dll")]
                  extern static int SendMessage(IntPtr hwnd, int id, int res, ref Point pos);

                  protected override void OnSizeChanged(EventArgs e)
                  {
                  ScrollToEnd();
                  base.OnSizeChanged (e);
                  }

                  //this scrolls the text to the last line when ever text is changed.
                  protected override void OnTextChanged(EventArgs e)
                  {
                  ScrollToEnd();
                  base.OnTextChanged (e);
                  }
                  }

                  top secret
                  Download xacc-ide 0.0.3 now!
                  See some screenshots

                  D 1 Reply Last reply
                  0
                  • L leppie

                    This has been asked 10 gazillion times, so here goes one solution:

                    public class RichTextBox : System.Windows.Forms.RichTextBox
                    {
                    void ScrollToEnd()
                    {
                    const int msg = 0x400 + 222;
                    int h = Height;
                    int y = Font.Height * Lines.Length - h;
                    Point point = new Point(0, y);
                    SendMessage(Handle, msg, 0, ref point);
                    }

                    [DllImport("user32.dll")]
                    extern static int SendMessage(IntPtr hwnd, int id, int res, ref Point pos);

                    protected override void OnSizeChanged(EventArgs e)
                    {
                    ScrollToEnd();
                    base.OnSizeChanged (e);
                    }

                    //this scrolls the text to the last line when ever text is changed.
                    protected override void OnTextChanged(EventArgs e)
                    {
                    ScrollToEnd();
                    base.OnTextChanged (e);
                    }
                    }

                    top secret
                    Download xacc-ide 0.0.3 now!
                    See some screenshots

                    D Offline
                    D Offline
                    Dennis C Dietrich
                    wrote on last edited by
                    #9

                    leppie wrote: This has been asked 10 gazillion times, so here goes one solution I don't see the advantage of using SendMessage instead of the RichTextBox's own methods. Why would you prefer that solution? Best regards Dennis

                    L 1 Reply Last reply
                    0
                    • D Dennis C Dietrich

                      leppie wrote: This has been asked 10 gazillion times, so here goes one solution I don't see the advantage of using SendMessage instead of the RichTextBox's own methods. Why would you prefer that solution? Best regards Dennis

                      L Offline
                      L Offline
                      leppie
                      wrote on last edited by
                      #10

                      Because the methods suppliedin RichTextBox, doesnt work if the control dont have focus, also focusing the control, brings in a host of other issues. Try it! :p top secret
                      Download xacc-ide 0.0.3 now!
                      See some screenshots

                      D 1 Reply Last reply
                      0
                      • L leppie

                        Because the methods suppliedin RichTextBox, doesnt work if the control dont have focus, also focusing the control, brings in a host of other issues. Try it! :p top secret
                        Download xacc-ide 0.0.3 now!
                        See some screenshots

                        D Offline
                        D Offline
                        Dennis C Dietrich
                        wrote on last edited by
                        #11

                        leppie wrote: Because the methods suppliedin RichTextBox, doesnt work if the control dont have focus, also focusing the control, brings in a host of other issues. Try it! Well, I did. Where is no need to set the focus. Not with the .NET Framework 2.0. ;-) I know it's just the first Beta but hopefully this change will also be in the RTM. Best regards Dennis

                        L 2 Replies Last reply
                        0
                        • D Dennis C Dietrich

                          leppie wrote: Because the methods suppliedin RichTextBox, doesnt work if the control dont have focus, also focusing the control, brings in a host of other issues. Try it! Well, I did. Where is no need to set the focus. Not with the .NET Framework 2.0. ;-) I know it's just the first Beta but hopefully this change will also be in the RTM. Best regards Dennis

                          L Offline
                          L Offline
                          leppie
                          wrote on last edited by
                          #12

                          Dennis C. Dietrich wrote: Not with the .NET Framework 2.0. Most people deal with .NET 1.1 unless otherwise specified, what is the point of mentioning it will be fixed, if it is broken currently? top secret
                          Download xacc-ide 0.0.3 now!
                          See some screenshots

                          D 1 Reply Last reply
                          0
                          • D Dennis C Dietrich

                            leppie wrote: Because the methods suppliedin RichTextBox, doesnt work if the control dont have focus, also focusing the control, brings in a host of other issues. Try it! Well, I did. Where is no need to set the focus. Not with the .NET Framework 2.0. ;-) I know it's just the first Beta but hopefully this change will also be in the RTM. Best regards Dennis

                            L Offline
                            L Offline
                            leppie
                            wrote on last edited by
                            #13

                            Your method also fails to consider that text might be selected in the RTB, so much more work to handle that, now add some color and threads, and you have a nice mess. top secret
                            Download xacc-ide 0.0.3 now!
                            See some screenshots

                            D 1 Reply Last reply
                            0
                            • L leppie

                              Dennis C. Dietrich wrote: Not with the .NET Framework 2.0. Most people deal with .NET 1.1 unless otherwise specified, what is the point of mentioning it will be fixed, if it is broken currently? top secret
                              Download xacc-ide 0.0.3 now!
                              See some screenshots

                              D Offline
                              D Offline
                              Dennis C Dietrich
                              wrote on last edited by
                              #14

                              leppie wrote: Most people deal with .NET 1.1 unless otherwise specified, what is the point of mentioning it will be fixed, if it is broken currently? It depends. In case someone needs the functionality right now there is no point. However if someone just started a project which is supposed to be finished in one or two years it would already be worth considering .NET 2.0 specific solutions. Besides it doesn't hurt to learn about new features as soon as possible (that might keep one from using obsolete code later). Best regards Dennis

                              1 Reply Last reply
                              0
                              • L leppie

                                Your method also fails to consider that text might be selected in the RTB, so much more work to handle that, now add some color and threads, and you have a nice mess. top secret
                                Download xacc-ide 0.0.3 now!
                                See some screenshots

                                D Offline
                                D Offline
                                Dennis C Dietrich
                                wrote on last edited by
                                #15

                                leppie wrote: Your method also fails to consider that text might be selected in the RTB, so much more work to handle that, now add some color and threads, and you have a nice mess. Yeah, you're right. But how am I supposed to know if that's required? I guess that's the whole point here. You contributed a solution, I contributed a solution. Yours is a general purpose one. However it depends on Win32 API calls (and therefore isn't platform-independent... yeah, I know... I don't know if that's required either ;-)). Mine has some drawback with complex states of the RichTextBox. But following the Simplicity XP rule[^] (as well as the KISS principle[^]) ScrollToCaret() would still be my first suggestion. If it turns out not to meet the requirements it has to be re-written. You see, if one starts with the more complex solution and it is unnecessary someone might replace it during refactoring anyway (following the Refactor Mercilessly rule[^]). Best regards Dennis

                                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