Example of automatic scrolling of WinForm textbox in C #

Source: Internet
Author: User

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 ();
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.