By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,146 Members | 1,387 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,146 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

Jan 25 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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.
Jan 26 '07 #2

P: n/a


On Jan 25, 4:16 pm, "Pete" <m...@ineedhelp.orgwrote:
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.

Jan 26 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.