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

going to last line of text in a RichTextBox.

P: n/a
I have a RichTextBox called rtbOutput.

I am adding lines of text using the AppendText method. What I want to do is
immediately after adding this line of text make sure that this line of text
is displayed, effictively scrolling to the last line of the text box all the
time.

Can anyone help as I cannot seem to find a way to do this.

Thanks

Martin
Nov 15 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Hi, Martin Dew,

There is nothing ready in the framework to help you. You can use the
following code to do what you requested:

// Attach to the TextChanged event of the
// RichTextBox you have the following method.
void richTextBox1_TextChanged(object s, EventArgs e)
{
// Either the identifier of your RichTextBox should be called
// richTextBox1 or replace richTextBox1 with the identifier
// of your RichTextBox or uncomment the following line
// RichTextBox richTextBox1 = s as RichTextBox;
int min, max;
GetScrollRange(richTextBox1.Handle, SB_VERT, out min, out max);
SendMessage(richTextBox1.Handle, EM_SETSCROLLPOS, 0, new POINT(0, max -
richTextBox1.Height));
}

const int SB_VERT = 1;
const int EM_SETSCROLLPOS = 0x0400 + 222;

[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);

[StructLayout(LayoutKind.Sequential)]
class POINT
{
public int x;
public int y;

public POINT() { }

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

You can move this code in some utility class to reuse it easily. Just set
the accessibility level of everything to public.

Greetings
Martin
"Martin Dew" <re************************@adastra.co.uk> wrote in message
news:um**************@tk2msftngp13.phx.gbl...
I have a RichTextBox called rtbOutput.

I am adding lines of text using the AppendText method. What I want to do is immediately after adding this line of text make sure that this line of text is displayed, effictively scrolling to the last line of the text box all the time.

Can anyone help as I cannot seem to find a way to do this.

Thanks

Martin

Nov 15 '05 #2

P: n/a
<snip>

Thanks Martin. That works perfectly.
Nov 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.