C# WinForm RichTextBox文本动态滚动显示文本方式
作者:楚楚3107 发布时间:2021-09-12 20:44:31
标签: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;
}
}
来源:https://blog.csdn.net/chulijun3107/article/details/79698020


猜你喜欢
- 目标效果: 点击动画按钮之后每张牌各自旋转 散开到屏幕上半部分的任意位置之后回到初始位置 比较像LOL男刀的技能动画 : )1: 创建卡牌对
- 使用IDEA开发微服务项目,需要启动多个微服务,可以开启IDEA的Run DashBoard窗口,需要对IDEA中指定工程的父工程进行配置进
- 最近在搞一个购物车的功能,里面有一个批量删除的操作,采用的是ExpandableListView以及BaseExpandableListAd
- JVM应用度量框架Micrometer实战前提spring-actuator做度量统计收集,使用Prometheus(普罗米修斯)进行数据收
- 一、 java文件中获得路径Thread.currentThread().getContextClassLoader().getResour
- (由于篇幅原因阐述的不够详细科学,不喜勿喷)。经常看到java中对byte数组的不同定义,粗略整理的一下:一个字节(byte)=8位(bit
- Spring @RequestParam对象绑定在Spring中,如果在方法参数列表中使用@RequestParam标注多个参数,会让映射方
- 代码如下所示:using System;using System.Collections.Generic;using System.Text
- 不想废话,直接写了!因为是留给自己做随笔的,所以大神们看到别喷…… 1.必须有微信公众账号 2.你也可以申请测试微信号,
- Java 中的运算符与 C 语言基本一致。1、算术运算符操作符描述例子+加法 : 相加运算符两侧的值A + B 等于 30-减法 : 左操作
- C++中一个重要的特性就是指针,指针不仅具有获得地址的能力,还具有操作地址的能力。指针可以用于数组、或作为函数的参数,用来访问内存和对内存的
- 需要5个类:1.实体类:Person.java2.抽象类:SQLOperate.java(封装了对数据库的操作)3.助手类:DBOpenHe
- 在web页面上我们可以通过frameset,iframe嵌套框架很容易实现各种导航+内容的布局界面,而在winform、WPF中实现其实也很
- 页面元素定位是自动化中最重要的事情, selenium Webdriver 提供了很多种元素定位的方法。 测试人员应该熟练掌握各
- 下载Android SDK两种方式:(1)官网下载(需翻墙):https://developer.android.com/studio/in
- 前言C#中提供了比较全面的字符串处理方法,很多函数都进行了封装为我们的编程工作提供了很大的便利。System.String是最常用的字符串操
- 好程序员Java教程分享MyBatis Plus介绍:1.MyBatis Plus 介绍MyBatis Plus 是国内人员开发的 MyBa
- multipartResolver上传文件配置1、gradle配置 compile ('commons-i
- 引言ApplicationEvent以及Listener是Spring为我们提供的一个事件监听、订阅的实现,内部实现原理是观察者设计模式,设
- 在 fluro 中,定义路由处理器 Handler 时可以指定该页面的默认转场形式,或者在使用 navigateTo 方法是可以设置页面跳转