溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么使用C#?WinForm?RichTextBox文本動態(tài)滾動顯示文本

發(fā)布時間:2023-03-01 09:55:37 來源:億速云 閱讀:192 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹“怎么使用C# WinForm RichTextBox文本動態(tài)滾動顯示文本”的相關(guān)知識,小編通過實(shí)際案例向大家展示操作過程,操作方法簡單快捷,實(shí)用性強(qiáng),希望這篇“怎么使用C# WinForm RichTextBox文本動態(tài)滾動顯示文本”文章能幫助大家解決問題。

    WinForm RichTextBox文本動態(tài)滾動顯示文本方

    在RichTextBox動態(tài)顯示一些文本信息時,需要一些設(shè)置,顯示當(dāng)前要顯示的字符串。

    怎么使用C#?WinForm?RichTextBox文本動態(tài)滾動顯示文本

    一個RichTextBox,一個按鈕。

    下圖為運(yùn)行時顯示過程中

    怎么使用C#?WinForm?RichTextBox文本動態(tài)滾動顯示文本

    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();//滾動到控件光標(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)容滾動打印中!!!\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;
        }
    }

    關(guān)于“怎么使用C# WinForm RichTextBox文本動態(tài)滾動顯示文本”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識,可以關(guān)注億速云行業(yè)資訊頻道,小編每天都會為大家更新不同的知識點(diǎn)。

    向AI問一下細(xì)節(jié)

    免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

    AI