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
0
投稿
猜你喜欢
- AudioSource 组件参考属性属性说明Clip音频资源Volume音量大小Mute是否静音Loop是否循环Play on load加载
- 本文实例讲述了Java链表中元素删除的实现方法。分享给大家供大家参考,具体如下:该部分与上一节是息息相关的,关于如何在链表中删除元素,我们一
- 泛型是Java中一个非常重要的内容,对于Java进阶学习是必须要掌握的知识点之所以说这个知识点重要,如果你有过阅读过一些开源框架的代码,那你
- 本文实例讲述了C#资源释放方法。分享给大家供大家参考,具体如下:1、try{}finally{}2、using只有类型实现了IDisposa
- 只能输入数字:"^[0-9]*$"。只能输入n位的数字:"^\d{n}$"。只能输入至少n位的数字:
- 大多数的B2C商城项目都会有限时活动,当用户下单后都会有支付超时时间,当订单超时后订单的状态就会自动变成已取消 ,这个功能的实现
- 本文的主要内容包括在下图,下面来一起看看吧。1、&和&&的区别2、switchswitch语句能否作用在byte,能
- 本文实例讲述了WCF实现的计算器功能。分享给大家供大家参考,具体如下:对于WCF,我们有了前面的理论基础,今天通过一个计算器的实例主要给大家
- 这篇文章主要介绍了Java多态中动态绑定原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以
- 前言相信很多Java开发都遇到过一个面试题:Resource和Autowired的区别是什么?这个问题的答案相信基本都清楚,但是这两者在Sp
- 0、前言本文主要对几种常见Java序列化方式进行实现。包括Java原生以流的方法进行的序列化、Json序列化、FastJson序列化、Pro
- 1.查找文件find / -name filename.txt 根据名称查找/目录下的filename.txt文件。find . -name
- 1.对原生态jdbc程序中问题总结1.1 jdbc程序需求:使用jdbc查询mysql数据库中用户表的记录statement:向数据库中发送
- 本文实例为大家分享了Java操作MongoDB模糊查询和分页查询,供大家参考,具体内容如下模糊查询条件:1、完全匹配Pattern patt
- 在Java移动文件夹及其所有子文件与子文件夹可以有如下的一段简单的方法来说明:public static void moveFolder(S
- 本文实例讲述了Java数组的定义、初始化、及二维数组用法。分享给大家供大家参考,具体如下:数组的定义1.数组是有序数据的集合,数组中的每个元
- 本项目使用的环境:开发工具:Intellij IDEA 2017.1.3springboot: 1.5.6jdk:1.8.0_161mave
- 前言String 类在Java中是很常用的类,很重要的类,在后续的学习中经常会用到,是后续学习的基础一、认识String1.JDK中的Str
- 本文实例为大家分享了Unity苹果手机Taptic震动的具体代码,供大家参考,具体内容如下文件:ios震动.zip将上方文件解压之后将Mul
- 1、写在前面今天群里一个小伙伴问了这样一个问题,扩展方法与实例方法的执行顺序是什么样子的,谁先谁后(这个问题会在文章结尾回答)。所以写了这边