怎么使用C# WinForm RichTextBox文本动态滚动显示文本

发布时间:2023-03-01 09:55:37 作者:iii
来源:亿速云 阅读:190

这篇文章主要介绍“怎么使用C# WinForm RichTextBox文本动态滚动显示文本”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“怎么使用C# WinForm RichTextBox文本动态滚动显示文本”文章能帮助大家解决问题。

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

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

怎么使用C# WinForm RichTextBox文本动态滚动显示文本

一个RichTextBox,一个按钮。

下图为运行时显示过程中

怎么使用C# WinForm 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# WinForm RichTextBox文本动态滚动显示文本”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注亿速云行业资讯频道,小编每天都会为大家更新不同的知识点。

推荐阅读:
  1. C#基于winform实现音乐播放器的方法
  2. 如何使用C# winForm自定义弹出页面效果

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

winform richtextbox

上一篇:Python读取Hive数据库代码怎么写

下一篇:python魔法方法 __ slots __怎么使用

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》