keep it scrolled

  • Thread starter Thread starter M D
  • Start date Start date
M

M D

System.Windows.Forms.Textbox

Imagine a multiple line text box that never gets focus. For instance
the conversation box of a chat screen.

How can you keep it scrolled to the bottom as lines are added.
Textbox.ScrollToCaret() requires the control be in focus and I would
rather not be swapping around focus while users are, well, using.

thx
md
 
You can automatically scroll using the send message api

//constants
private const int WM_HSCROLL = 0x114
private const int WM_VSCROLL = 0x115
private const int SB_LINEUP = 0
private const int SB_LINELEFT = 0
private const int SB_LINEDOWN = 1
private const int SB_LINERIGHT = 1
private const int SB_PAGEUP = 2
private const int SB_PAGELEFT = 2
private const int SB_PAGEDOWN = 3
private const int SB_PAGERIGHT = 3
private const int SB_THUMBPOSITION = 4
private const int SB_THUMBTRACK = 5
private const int SB_TOP = 6
private const int SB_LEFT = 6
private const int SB_BOTTOM = 7
private const int SB_RIGHT = 7
private const int SB_ENDSCROLL = 8


[DllImport("user32.dll")]
private static extern int SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam,
IntPtr lParam);

Usage -

//when you want the textbox to scroll bottom do this
SendMessage(textBox1.Handle, WM_VSCROLL, (IntPtr) SB_BOTTOM, IntPtr.Zero);
 
Back
Top