Scroll to the end of a text box.

  • Thread starter Thread starter UJ
  • Start date Start date
U

UJ

I've got a multiline textbox that I'm using to display messages. Is there an
easy way to have it scroll to the end after I add a line to the text of the
box?

TIA - Jeff.
 
UJ said:
I've got a multiline textbox that I'm using to display messages. Is there an
easy way to have it scroll to the end after I add a line to the text of the
box?

TIA - Jeff.

The only way I've found, is to use Windows messages ... Not nice but the only
solution I ever found.


using System.Runtime.InteropServices;

[DllImport("user32.dll", EntryPoint="SendMessageA")]
static extern uint SendMessage(System.IntPtr hwnd, uint wMsg, uint wParam, uint
lParam);

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

TextBox report ;

void printError(string msg)
{
...
// auto scroll to bottom of text control
SendMessage( this.report.Handle, WM_VSCROLL, SB_BOTTOM, 0);
}


cyrille
 
Jeff,

Here's a very simple technique that works with a RichTextBox; should
work with a regular TextBox also:

RichTextBox.Select(RichTextBox.TextLength, 0);
RichTextBox.ScrollToCaret();

Hope this helps,
John Bendiksen

# Cyrille37 # said:
UJ said:
I've got a multiline textbox that I'm using to display messages. Is there an
easy way to have it scroll to the end after I add a line to the text of the
box?

TIA - Jeff.

The only way I've found, is to use Windows messages ... Not nice but the only
solution I ever found.


using System.Runtime.InteropServices;

[DllImport("user32.dll", EntryPoint="SendMessageA")]
static extern uint SendMessage(System.IntPtr hwnd, uint wMsg, uint wParam, uint
lParam);

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

TextBox report ;

void printError(string msg)
{
...
// auto scroll to bottom of text control
SendMessage( this.report.Handle, WM_VSCROLL, SB_BOTTOM, 0);
}


cyrille
 
Jeff,

Here's a very simple technique that works with a RichTextBox; should
work with a regular TextBox also:

RichTextBox.Select(RichTextBox.TextLength, 0);
RichTextBox.ScrollToCaret();

Hope this helps,
John Bendiksen

# Cyrille37 # said:
UJ said:
I've got a multiline textbox that I'm using to display messages. Is there an
easy way to have it scroll to the end after I add a line to the text of the
box?

TIA - Jeff.

The only way I've found, is to use Windows messages ... Not nice but the only
solution I ever found.


using System.Runtime.InteropServices;

[DllImport("user32.dll", EntryPoint="SendMessageA")]
static extern uint SendMessage(System.IntPtr hwnd, uint wMsg, uint wParam, uint
lParam);

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

TextBox report ;

void printError(string msg)
{
...
// auto scroll to bottom of text control
SendMessage( this.report.Handle, WM_VSCROLL, SB_BOTTOM, 0);
}


cyrille
 
JohnAtAphelion said:
Jeff,

Here's a very simple technique that works with a RichTextBox; should
work with a regular TextBox also:

RichTextBox.Select(RichTextBox.TextLength, 0);
RichTextBox.ScrollToCaret();

hello,

I'm afraid that not the right solution.

Before found the solution with Windows Message, it was the first thing I've used.
But it does not work it the control has not got focus.

Hope this helps,
John Bendiksen

# Cyrille37 # said:
UJ said:
I've got a multiline textbox that I'm using to display messages. Is there an
easy way to have it scroll to the end after I add a line to the text of the
box?

TIA - Jeff.

The only way I've found, is to use Windows messages ... Not nice but the only
solution I ever found.


using System.Runtime.InteropServices;

[DllImport("user32.dll", EntryPoint="SendMessageA")]
static extern uint SendMessage(System.IntPtr hwnd, uint wMsg, uint wParam, uint
lParam);

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

TextBox report ;

void printError(string msg)
{
...
// auto scroll to bottom of text control
SendMessage( this.report.Handle, WM_VSCROLL, SB_BOTTOM, 0);
}


cyrille
 
Cyrille37,

Not sure why it didn't work for you...

In my 'process control' type application, I am updating 4 RichTextBoxes
continuously with status information (probably similar to Jeff's
situation). None of them ever have focus; in fact, they are not even
selectable by the user. They all scroll properly to the last added
text using this technique.

Happy coding,
John Bendiksen
 
I've got a multiline textbox that I'm using to display messages. Is there an
easy way to have it scroll to the end after I add a line to the text of the
box?

TIA - Jeff.

Set HideSelection to false.

Oz
 
Back
Top