C#?WinForm?RichTextBox文本動態(tài)滾動顯示文本方式
更新時間:2023年03月01日 08:35:15 作者:楚楚3107
這篇文章主要介紹了C#?WinForm?RichTextBox文本動態(tài)滾動顯示文本方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
WinForm RichTextBox文本動態(tài)滾動顯示文本方
在RichTextBox動態(tài)顯示一些文本信息時,需要一些設(shè)置,顯示當前要顯示的字符串。
一個RichTextBox,一個按鈕。
下圖為運行時顯示過程中
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)//其它線程調(diào)用 { delInfoList d = new delInfoList(SetrichTextBox); richTextBox1.Invoke(d, value); } else//本線程調(diào)用 { if (richTextBox1.Lines.Length > 100) { richTextBox1.Clear(); } richTextBox1.Focus(); //讓文本框獲取焦點 richTextBox1.Select(richTextBox1.TextLength, 0);//設(shè)置光標的位置到文本尾 richTextBox1.ScrollToCaret();//滾動到控件光標處 richTextBox1.AppendText(value);//添加內(nèi)容 } } private void button1_Click(object sender, EventArgs e) { for (int i = 0; i < 300; i++) { SetrichTextBox(DateTime.Now.ToString() + " 內(nèi)容滾動打印中!!!\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 = "開始測試"; 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 = "滾動打印測試"; 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; } }
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
C#實現(xiàn)帶行數(shù)和標尺的RichTextBox
這篇文章主要為大家詳細介紹了如何利用C#實現(xiàn)帶行數(shù)和標尺的RichTextBox,文中的示例代碼講解詳細,對我們學習C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下2022-12-12