When RichTextBox dynamically displays some text information, some settings are required to display the current string to be displayed.
A RichTextBox, a button. The following figure shows the display process at runtime.
Form1.cs:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace RichTextBoxScroll { public partial class Form1 : Form { private delegate void delInfoList(string text); public Form1() { InitializeComponent(); } private void SetrichTextBox(string value) { if (richTextBox1.InvokeRequired)//Other thread calls { delInfoList d = new delInfoList(SetrichTextBox); richTextBox1.Invoke(d, value); } else//This thread calls { if (richTextBox1.Lines.Length > 100) { richTextBox1.Clear(); } richTextBox1.Focus(); //Let the text box get the focus richTextBox1.Select(richTextBox1.TextLength, 0);//Set the cursor position to the end of the text richTextBox1.ScrollToCaret();//Scroll to the control cursor richTextBox1.AppendText(value);//Add content } } private void button1_Click(object sender, EventArgs e) { for (int i = 0; i < 300; i++) { SetrichTextBox(DateTime.Now.ToString() + "The content is scrolling and printing!!!\n"); } } } }
Form1.Designer.cs:
namespace RichTextBoxScroll { partial class Form1 { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.panel1 = new System.Windows.Forms.Panel(); this.button1 = new System.Windows.Forms.Button(); this.richTextBox1 = new System.Windows.Forms.RichTextBox(); this.panel1.SuspendLayout(); this.SuspendLayout(); // // panel1 // this.panel1.Controls.Add(this.button1); this.panel1.Controls.Add(this.richTextBox1); this.panel1.Dock = System.Windows.Forms.DockStyle.Fill; this.panel1.Location = new System.Drawing.Point(0, 0); this.panel1.Name = "panel1"; this.panel1.Size = new System.Drawing.Size(706, 496); this.panel1.TabIndex = 0; // // button1 // this.button1.Location = new System.Drawing.Point(609, 85); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(75, 23); this.button1.TabIndex = 1; this.button1.Text = "Start Test"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); // // richTextBox1 // this.richTextBox1.Dock = System.Windows.Forms.DockStyle.Fill; this.richTextBox1.Font = new System.Drawing.Font("SimSun", 21.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.richTextBox1.Location = new System.Drawing.Point(0, 0); this.richTextBox1.Name = "richTextBox1"; this.richTextBox1.Size = new System.Drawing.Size(706, 496); this.richTextBox1.TabIndex = 0; this.richTextBox1.Text = ""; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(706, 496); this.Controls.Add(this.panel1); this.Name = "Form1"; this.Text = "Scroll Printing Test"; this.panel1.ResumeLayout(false); this.ResumeLayout(false); } #endregion private System.Windows.Forms.Panel panel1; private System.Windows.Forms.RichTextBox richTextBox1; private System.Windows.Forms.Button button1; } }