C# WinForm RichTextBox文本动态滚动显示文本方式

 更新时间:2023年03月01日 08:35:15   作者:楚楚3107  
这篇文章主要介绍了C# WinForm RichTextBox文本动态滚动显示文本方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

WinForm RichTextBox文本动态滚动显示文本方

在RichTextBox动态显示一些文本信息时,需要一些设置,显示当前要显示的字符串。

一个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)//其它线程调用
            {
                delInfoList d = new delInfoList(SetrichTextBox);
                richTextBox1.Invoke(d, value);
            }
            else//本线程调用
            {
                if (richTextBox1.Lines.Length > 100)
                { 
                    richTextBox1.Clear();
                }
 
                richTextBox1.Focus(); //让文本框获取焦点 
                richTextBox1.Select(richTextBox1.TextLength, 0);//设置光标的位置到文本尾
                richTextBox1.ScrollToCaret();//滚动到控件光标处 
                richTextBox1.AppendText(value);//添加内容
            }
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < 300; i++)
            {
                SetrichTextBox(DateTime.Now.ToString() + " 内容滚动打印中!!!\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;
    }
}

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • C#中DateTime函数的详细用法

    C#中DateTime函数的详细用法

    这篇文章介绍了C#中DateTime函数的详细用法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-06-06
  • 浅谈C#多线程简单例子讲解

    浅谈C#多线程简单例子讲解

    本篇文章主要介绍了C#多线程简单例子,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-12-12
  • C# IQueryable<T>揭开表达式树的神秘面纱

    C# IQueryable<T>揭开表达式树的神秘面纱

    这篇文章主要介绍了C# IQueryable<T>表达式树,对IQueryable<T>感兴趣的同学,必须要仔细看一下
    2021-04-04
  • unity 鼠标移入弹出UI的操作

    unity 鼠标移入弹出UI的操作

    这篇文章主要介绍了unity 鼠标移入弹出UI的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-04-04
  • C#在复杂多线程环境下使用读写锁同步写入文件

    C#在复杂多线程环境下使用读写锁同步写入文件

    这篇文章介绍了C#在复杂多线程环境下使用读写锁同步写入文件的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-04-04
  • 如何用.NETCore操作RabbitMQ

    如何用.NETCore操作RabbitMQ

    这篇文章主要介绍了如何用.NETCore操作RabbitMQ,对中间件感兴趣的同学,可以参考下
    2021-05-05
  • C#中 城市线路图的纯算法以及附带求极权值

    C#中 城市线路图的纯算法以及附带求极权值

    本篇文章介绍了,在C#中城市线路图的纯算法以及附带求极权值的方法,需要的朋友参考下
    2013-04-04
  • C#异步原理详情

    C#异步原理详情

    这篇文章主要介绍了C#异步原理,C#异步在C#5便发布了,是利用async关键字和await表达式表达的异步操作,下面文章下边将详细向大家介绍C#异步原理,感兴趣得小伙伴可以参考一下
    2021-10-10
  • C#利用IDbDataAdapter/IDataReader实现通用数据集获取

    C#利用IDbDataAdapter/IDataReader实现通用数据集获取

    这篇文章主要为大家详细介绍了C#利用IDbDataAdapter/IDataReader实现通用数据集获取的相关知识,感兴趣的小伙伴可以跟随小编一起学习一下
    2024-11-11
  • C#如何将Excel转换为PDF

    C#如何将Excel转换为PDF

    这篇文章主要介绍了C#如何将Excel转换为PDF问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-01-01

最新评论