【WinForm】 RichTextBox使用汇总

一、文本动态滚动显示文本

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

 

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

 

二、让水平滚动条出现

将控件wordwrap属性设置为false

 

三、内容文字大小显示不一

rtb.LanguageOption = System.Windows.Forms.RichTextBoxLanguageOptions.UIFonts;

 

四、取消内容选中状态

// 重置选定范围
richTextBox1.DeselectAll();
posted @ 2020-09-01 17:04  不溯流光  阅读(656)  评论(0编辑  收藏  举报