This question comes from the forum questions, the demo code is as follows
Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Text;
Using System.Windows.Forms;
Using System.Runtime.InteropServices;
Namespace WindowsApplication27
... {
/**////<summary>
Demonstrates how to scroll the text in a TextBox by looping it:
Author Jinjazz
Author Blog:http://blog.csdn.net/jinjazz
</summary>
public partial class Form1:form
... {
Public Form1 ()
... {
InitializeComponent ();
This.textBox1.Clear ();
for (int i = 0; I <= 20;i++)
... {
This.textBox1.Text = = String. Format ("{0}:jinjazz__{1}", i,i);
}
This.timer1.Interval = 200;
This.timer1.Start ();
}
Send a message
[DllImport ("user32.dll", EntryPoint = "SendMessage")]
public static extern int SendMessage (IntPtr hWnd, int wmsg, int wParam, int lParam);
Get scroll bar position
[DllImport ("user32")]
public static extern int GetScrollPos (INTPTR hwnd, int nbar);
Set scroll bar position
[DllImport ("user32.dll")]
static extern int SetScrollPos (IntPtr hWnd, int nbar,
int NPOs, bool bredraw);
public const int em_linescroll = 0XB6;
private void Timer1_Tick (object sender, EventArgs e)
... {
int i= GetScrollPos (this.textbox1.handle,1);
Scroll down one line
SendMessage (This.textBox1.Handle, Em_linescroll, 0, 1);//0,1 scroll down on behalf of the vertical scroll bar
To determine whether there is a change in position, if not the bottom of the description to return to the beginning
if (i = = GetScrollPos (this.textBox1.Handle, 1))
... {
Back to the top, there seems to be a problem with setscrollpos, scroll bars and text are not synchronized updates
This.textBox1.SelectionStart = 0;
This.textBox1.SelectionLength = 1;
This.textBox1.ScrollToCaret ();
this.textBox1.SelectionLength = 0;
}
Console.WriteLine (i);
}
private void Textbox1_mouseenter (object sender, EventArgs e)
... {
This.timer1.Stop ();
}
private void Textbox1_mouseleave (object sender, EventArgs e)
... {
This.timer1.Start ();
}
}
}