Example of automatic scrolling of textbox loop in winform in C #

Source: Internet
Author: User

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>
/// Demonstrate how to scroll the text loop in TextBox:
/// </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 );
// Obtain the position of the scroll bar
[DllImport ("user32")]
Public static extern int GetScrollPos (IntPtr hwnd, int nBar );
// Set the 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 timereffectick (object sender, EventArgs e)
{
Int I = GetScrollPos (this. textBox1.Handle, 1 );

// Scroll down a row
SendMessage (this. textBox1.Handle, EM_LINESCROLL, 0, 1); // indicates vertical scroll down

// Determine whether there is any location change. If not, it indicates that it is at the bottom and returns the start point.
If (I = GetScrollPos (this. textBox1.Handle, 1 ))
{
// Return to the top. It seems that there is a problem with SetScrollPos. the scroll bar and text are not synchronously updated.
This. textBox1.SelectionStart = 0;
This. textBox1.SelectionLength = 1;
This. textBox1.ScrollToCaret ();
This. textBox1.SelectionLength = 0;
}
Console. WriteLine (I );
}

Private void textbox#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.