views:

2777

answers:

5

I am using winforms, and I update a text box once in a while (showing messages). however, when the text reaches the end of the box it produces scrollbars and I don't know how to scroll down to the bottom. The only thing I see is ScrollToCaret, but Caret is at the beginning of the text. What is the command to scroll?

+3  A: 

You need to set your caret at the end of your text:

textBox1.Text += "your new text";
textBox1.Select(textBox1.Text.Length - 1, 0);
textBox1.ScrollToCaret();
Francis B.
+8  A: 

Hi,

You can do this by making use of a function called ScrollToCaret. You need to first set the caret position to the end of the text box, then you can scroll to it. Here's how to do it:

        //move the caret to the end of the text
        textBox.SelectionStart = textBox.TextLength;
        //scroll to the caret
        textBox.ScrollToCaret();
DoctaJonez
ScrollToCaret() will only work if the text box in question has focus. See below for a solution handling the text box not having focus as well: http://stackoverflow.com/questions/1069497/2585177#2585177
Nailuj
Surely if that is indeed the case then a call to TextBox.Focus beforehand would work. No need to use pinvoke.
DoctaJonez
Yes, that is possible. But there might very well be cases where you would like to keep the focus somewhere else in your form while scrolling the TextBox. And if that's the case, you would have to implement extra logic for "re-focusing" the correct item after you have done this. But now I'm only being picky ;)
Nailuj
A: 

You can use the SetScrollPos API :

[DllImport("user32.dll")]
static extern int SetScrollPos(IntPtr hWnd, int nBar, int nPos, bool bRedraw);

[DllImport("user32.dll")]
static extern bool GetScrollRange(IntPtr hWnd, int nBar, out int lpMinPos, out int lpMaxPos);

const int SB_HORZ = 0;
const int SB_VERT = 1;
const int SB_CTL = 2;


...

void ScrollToBottom(Control ctl)
{
    int min;
    int max;
    if (GetScrollRange(ctl.Handle, SB_VERT, out min, out max))
    {
        SetScrollPos(ctl.Handle, SB_VERT, max, true);
    }
}

(untested)

Thomas Levesque
This doesn't acutally scroll the textbox, it just changes the position of the little button on the scrollbar.
Hannes Nel
+1  A: 

If you use the AppendText() method of the Textbox, the text will be added to the bottom of any existing text and the control will scroll to display it.

Metro
+1  A: 

This is a bit of an old question, but none of the suggested answers worked for me (ScrollToCaret() only works when the TextBox has focus). So in case any more should be searching for this at some point, I thought I'd share what I found to be the solution:

public class Utils
{
    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    private static extern int SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam);

    private const int WM_VSCROLL = 0x115;
    private const int SB_BOTTOM = 7;

    /// <summary>
    /// Scrolls the vertical scroll bar of a multi-line text box to the bottom.
    /// </summary>
    /// <param name="tb">The text box to scroll</param>
    public static void ScrollToBottom(TextBox tb)
    {
        SendMessage(tb.Handle, WM_VSCROLL, (IntPtr)SB_BOTTOM, IntPtr.Zero);
    }
}

Credit for the solution should go to this post at bytes.com: http://bytes.com/topic/c-sharp/answers/248500-scroll-bottom-textbox#post1005377

Nailuj