C#?WinForm?RichTextBox文本動(dòng)態(tài)滾動(dòng)顯示文本方式
WinForm RichTextBox文本動(dòng)態(tài)滾動(dòng)顯示文本方
在RichTextBox動(dòng)態(tài)顯示一些文本信息時(shí),需要一些設(shè)置,顯示當(dāng)前要顯示的字符串。
一個(gè)RichTextBox,一個(gè)按鈕。
下圖為運(yùn)行時(shí)顯示過(guò)程中
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(); //讓文本框獲取焦點(diǎn) richTextBox1.Select(richTextBox1.TextLength, 0);//設(shè)置光標(biāo)的位置到文本尾 richTextBox1.ScrollToCaret();//滾動(dòng)到控件光標(biāo)處 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)容滾動(dòng)打印中!!!\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 = "開(kāi)始測(cè)試"; 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 = "滾動(dòng)打印測(cè)試"; 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é)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
C#實(shí)現(xiàn)簡(jiǎn)易畫(huà)圖板的示例代碼
這篇文章主要介紹了C#實(shí)現(xiàn)簡(jiǎn)易畫(huà)圖板的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04C#代碼實(shí)現(xiàn)短信驗(yàn)證碼接口示例
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)短信驗(yàn)證碼接口示例代碼,感興趣的小伙伴們可以參考一下2016-08-08C#實(shí)現(xiàn)DVD借出歸還管理系統(tǒng)
這篇文章主要介紹了C#實(shí)現(xiàn)DVD借出歸還管理系統(tǒng),類(lèi)似DVD管理器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06C#設(shè)置程序開(kāi)機(jī)啟動(dòng)的實(shí)現(xiàn)示例
本文主要介紹了C#設(shè)置程序開(kāi)機(jī)啟動(dòng)的實(shí)現(xiàn)示例,可以通過(guò)修改注冊(cè)表將啟動(dòng)信息寫(xiě)入注冊(cè)表來(lái)實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2024-01-01C#實(shí)現(xiàn)帶行數(shù)和標(biāo)尺的RichTextBox
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)帶行數(shù)和標(biāo)尺的RichTextBox,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下2022-12-12C# 特性AttributeUsage簡(jiǎn)介與使用教程
這篇文章主要介紹了C# 特性AttributeUsage簡(jiǎn)介與使用教程,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-05-05