How to keep a multiline + vertical scrollbar textbox on the LAST line?

S

sherifffruitfly

Hi all,

I'm using a multiline+scrollbar textbox to log output as my program
runs. Basically, I pass a reference to the class instance that's
currently doing stuff, and the instance can tack on whatever it needs
to the text property.

Initially I had trouble with getting the textbox to refresh
as-the-program ran, but the TextBox.Refresh() method took care of that
for me.

Now the problem is that the output lines run longer than the visible
textbox - so the most recent messages can't be seen until the routine
is completely finished.

I would like the textbox to "auto-scroll-to-the-bottom-line" every time
I tack a new line into it. How can I do this?

Thanks for any ideas,

cdj
 
P

Pete

I had to do this once a long time back. After searching and searching the
only solution I could find was to do what is posted below. Also, I ended up
using a RichTextBox as it's buffer would hold more data.

I don't claim to know if this is the best way, but it was the only way I
could find via google and asking around a couple years ago, and it ended up
working.


I had to p/invoke 2 win32 functions and create a point class:

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

[DllImport("user32", CharSet=CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, POINT
lParam);

class POINT
{
public int x;
public int y;
public POINT()
{
}
public POINT(int x, int y)
{
this.x = x;
this.y = y;
}
}

Then, using my RichTextBox as below in a function where I passed in a
string:

RichTextBox1.SelectionStart = RichTextBox1.TextLength + 1;
RichTextBox1.AppendText(string);
GetScrollRange(RichTextBox1.Handle, SB_VERT, out min, out max);
SendMessage(RichTextBox1.Handle, EM_SETSCROLLPOS, 0, new POINT(0, max -
RichTextBox1.Height));
RichTextBox1.Update();


Hope that helps.
 
S

sherifffruitfly

I had to p/invoke 2 win32 functions and create a point class:

Thanks for the suggestion. After digging around, I found that using
TextBox.AppendText(string text), rather than TextBox.Text += text gives
the auto-scroll-to-bottom I needed - and much easier than going down to
the WinAPI.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top