Scroll after appendText in RichTextBox

J

Joseph Lee

Hi,

I am trying to find the command to scroll the richtextbox to the latest
appended string at the bottom. I have tried update, refresh and scroll to
caret but it does not seem to work

Thanks

Joey
 
A

Arne Janning

Joseph said:
I am trying to find the command to scroll the richtextbox to the latest
appended string at the bottom. I have tried update, refresh and scroll to
caret but it does not seem to work

Hi Joseph,

it only works if the RTF-Box has the focus (and therefore a caret).

Is that is a problem for you here is a small workaround using WinAPI
SendMessage:

using System.Runtime.InteropServices;

[...]

//WinAPI-Declaration for SendMessage
[DllImport("user32.dll")]
public static extern IntPtr SendMessage(
IntPtr window, int message, int wparam, int lparam);

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


private void button1_Click(object sender, System.EventArgs e)
{
for (int i = 0; i < 100; i++)
{
//Let's add some text to the rtfbox
richTextBox1.Text += "Hallo Welt ";
}
//Scroll to the bottom, without focus
SendMessage(richTextBox1.Handle, WM_VSCROLL, SB_BOTTOM, 0);
}

Cheers

Arne Janning
 
J

Joseph Lee

Yes, that was what I am searching for.
Thanks :)

Joey

Arne Janning said:
Joseph said:
I am trying to find the command to scroll the richtextbox to the latest
appended string at the bottom. I have tried update, refresh and scroll to
caret but it does not seem to work

Hi Joseph,

it only works if the RTF-Box has the focus (and therefore a caret).

Is that is a problem for you here is a small workaround using WinAPI
SendMessage:

using System.Runtime.InteropServices;

[...]

//WinAPI-Declaration for SendMessage
[DllImport("user32.dll")]
public static extern IntPtr SendMessage(
IntPtr window, int message, int wparam, int lparam);

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


private void button1_Click(object sender, System.EventArgs e)
{
for (int i = 0; i < 100; i++)
{
//Let's add some text to the rtfbox
richTextBox1.Text += "Hallo Welt ";
}
//Scroll to the bottom, without focus
SendMessage(richTextBox1.Handle, WM_VSCROLL, SB_BOTTOM, 0);
}

Cheers

Arne Janning
 

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