Thread: Scroll Bars Are Scrolling Me Crazy

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    17

    Scroll Bars Are Scrolling Me Crazy

    Okay, excuse the horrible pun on my part, but it's truly driving me insane.

    I'm self-learning C#. It's been fairly easy for me up to this point, and usually if I run into a problem the online help usually has the answer. But after spending two hours reading various things in there I don't know if I'm just overlooking it, or not understanding it. Point being, I can't figure out what I'm trying to do.

    Basically here's the situation:

    I have a multi-line text box that does not have scroll bars on by default. As the program is going along, it occasionally adds information into that text box. I have a check that runs to keep track of how many lines are currently in that box. Once the lines hit a certain #, I tell it to automatically add vertical scroll bars to the text box. So far, so good.

    this.txtInformationText.ScrollBars = ScrollBars.Vertical;


    The problem is, it'll continue to add the text into the box and the scroll bars aren't budging on their own. This may sound normal, and probably is, but I REALLY want it to scroll down with the information so the user doesn't have to scroll down with it. You might say "well, why not just keep only the most relevant info in the text box and eliminate the scroll bars all together" But I want the scroll bars there so that when the program has done its thing, the user can scroll up to see all that it's done, not just the most current information it has added.


    Anyway, I'm going crazy trying to figure out how to force it to scroll down. Does anyone out there have any ideas on how to do this? I will be forever thankful.

  2. #2
    Registered User Frobozz's Avatar
    Join Date
    Dec 2002
    Posts
    546
    If I recall, you use the SelectionStart property. Try setting it to the length of the text in the TextBox and see what happens.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    17
    Would it be used like this?

    this.txtInformationText.ScrollBars = ScrollBars.Vertical;
    this.txtInformationText.SelectionStart = windowLines;


    Or this?

    this.txtInformationText.ScrollBars = ScrollBars.Vertical;
    this.txtInformationText.SelectionStart = this.txtInformationText.Text.Length;



    Neither are working. :\
    Last edited by Iyouboushi; 03-02-2006 at 11:45 PM.

  4. #4
    Registered User Frobozz's Avatar
    Join Date
    Dec 2002
    Posts
    546
    The second. If I remember correctly, I did something similar back when I was using VB6. I basically passed the start of the selection the length of the text in the textbox. That was some time ago though so I'm not entirely sure.

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    17
    Yeah. You'd think something like that would work. It makes sense. Unless I'm just doing it wrong, it's not working at all though. It doesn't do anything. :\

  6. #6
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,110
    A multiline textbox implements the method ScrollToCaret(). This should be used in combination with AppendText(string text).

    Suppose we have a textbox called textBox1:
    Code:
    		private void textBox1_TextChanged(object sender, EventArgs e) {
    			this.textBox1.ScrollToCaret();
    		}
    
    		private void button1_Click(object sender, EventArgs e) {
    			for (int i = 0; i < 20; i++) {
    				this.textBox1.AppendText(i + "\r\n");
    			}
    		}
    When the button1 is pressed it will add the text and when the text is changed the eventHandler is called which will call scrollToCaret().

    If you would use this.textBox1.Text += i + "\r\n";
    it will not work.
    Don't ask me why thats just the way it is - although I think it has something to do with focussing on the control before calling ScrollToCaret(), msdn says the following:
    Quote Originally Posted by msdn
    Note This method has no effect if the control does not have focus or if the caret is already positioned in the visible region of the control.
    Anyway I hope this solves your problem.

    -Ganglylamb.
    Last edited by GanglyLamb; 03-03-2006 at 09:38 AM.

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    17
    That worked! Thank you so much!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Scroll bars
    By bigdan43 in forum C++ Programming
    Replies: 5
    Last Post: 04-19-2005, 12:05 PM
  2. Scroll Bars and Focus
    By Thantos in forum Windows Programming
    Replies: 1
    Last Post: 08-21-2003, 11:57 AM
  3. Horizontal Scroll Bars with CListBox
    By Malek in forum Windows Programming
    Replies: 1
    Last Post: 04-10-2003, 09:58 PM
  4. Bitmap scrolling with scroll bars
    By solar3147 in forum Windows Programming
    Replies: 0
    Last Post: 03-17-2003, 02:39 AM
  5. Scroll Bars
    By bc17 in forum Windows Programming
    Replies: 2
    Last Post: 02-21-2003, 03:15 PM