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

作者:楚楚3107 时间:2021-09-12 20:44:31 

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;
   }
}

来源:https://blog.csdn.net/chulijun3107/article/details/79698020

标签:C#,WinForm,RichTextBox,文本滚动
0
投稿

猜你喜欢

  • 详解JAVA流程控制语句

    2023-11-05 02:27:09
  • Spring AOP实现复杂的日志记录操作(自定义注解)

    2023-01-24 15:21:50
  • java异步编程之一文看完其异步函数表

    2021-12-25 10:57:39
  • C# DES加密算法中向量的作用详细解析

    2022-07-13 07:49:48
  • Spring+Junit4进行接口测试实例代码

    2021-09-15 07:44:06
  • springboot 如何解决yml没有spring的小叶子标志问题

    2021-06-12 02:57:01
  • 关于async和await的一些误区实例详解

    2022-09-22 22:57:29
  • spring cloud gateway 限流的实现与原理

    2023-04-10 16:47:56
  • Eclipse快速添加get、set方法的操作技巧

    2022-11-20 04:11:59
  • Java异常分类及统一处理详解

    2022-01-27 12:32:24
  • springMVC自定义注解,用AOP来实现日志记录的方法

    2023-11-29 13:58:53
  • Java 通过反射给实体类赋值操作

    2023-11-26 10:11:41
  • Java设计模式之模板方法模式Template Method Pattern详解

    2023-09-21 12:28:04
  • springboot返回前端中文乱码的解决

    2023-08-09 11:49:12
  • SpringBoot打Jar包在命令行运行流程详解

    2023-11-24 16:53:59
  • spring中的注解事务演示和添加步骤详情

    2023-03-03 08:32:48
  • SpringAOP 如何通过JoinPoint获取参数名和值

    2023-11-01 00:50:36
  • java制作简单验证码功能

    2022-12-07 02:12:46
  • 分享Android仿刮奖效果控件

    2021-12-15 16:00:16
  • 教你怎么在IDEA中创建java多模块项目

    2023-05-28 19:25:58
  • asp之家 软件编程 m.aspxhome.com