RichTextBox 富文本编辑控件

RichTextBox 富文本编辑控件

RichTextBox 是高级文本控件,提供高级文本输入和编辑功能,如字符或段落格式的设置。

常用属性

属性说明
DetectUrls是否自动将 URL 格式化为链接
EnableAutoDragDrop是否启用文本、图片和其他数据的拖放操作
BorderStyle边框样式
Lines多行文本行,以字符串数组形式返回
MaxLength最大可输入字符数
Multiline是否允许多行
ScrollBars滚动条行为(None、Horizontal、Vertical、Both、ForcedHorizontal、ForcedVertical、ForcedBoth)
WordWrap是否自动换行
Enabled是否启用
Text获取或设置纯文本内容
Rtf获取或设置包含 RTF 格式代码的文本
SelectionFont当前选中文本的字体系列
SelectionColor当前选中文本的颜色
SelectionAlignment当前段落的对齐方式
SelectionBullet当前段落是否使用项目符号
ZoomFactor缩放比例
this.richTextBox1.Multiline = true;
this.richTextBox1.ScrollBars = RichTextBoxScrollBars.Both;
this.richTextBox1.DetectUrls = true;
this.richTextBox1.EnableAutoDragDrop = true;
this.richTextBox1.ZoomFactor = 1.2f;Code language: JavaScript (javascript)

常用方法

方法说明
Cut()剪切选中内容到剪贴板
Copy()复制选中内容到剪贴板
Paste()从剪贴板粘贴内容
Paste(DataFormats.Format)按指定格式粘贴
Undo()撤销
Redo()重做
LoadFile(string)加载文件(支持 .txt、.rtf)
SaveFile(string)保存文件
Find(string)查找文本
Select(int, int)选中指定范围的文本
this.richTextBox1.LoadFile(@"C:\test.rtf");
this.richTextBox1.SaveFile(@"C:\output.rtf");Code language: JavaScript (javascript)

常用事件

事件说明
TextChanged文本改变时触发
SelectionChanged选中内容改变时触发
LinkClicked点击链接时触发
private void richTextBox1_LinkClicked(object sender, LinkClickedEventArgs e)
{
    System.Diagnostics.Process.Start(e.LinkText);
}Code language: JavaScript (javascript)

综合示例:简易富文本编辑器

通过 ToolStrip + Panel + RichTextBox + 对话框控件组合实现一个简易编辑器。

布局思路:

  1. 添加一个 Panel,将 ToolStrip 和 RichTextBox 放入其中
  2. 在 ToolStrip 上添加按钮,为每个按钮设置 Tag 标识
  3. 处理 toolStrip1_ItemClicked 事件,根据 Tag 执行不同操作

完整代码示例:

using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApp5
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private string rtf;
        /// <summary>
        /// 多信息文本格式内容
        /// </summary>
        public string Rtf
        {
            get { return this.richTextBox1.Rtf; }
            set { this.richTextBox1.Rtf = value; }
        }

        private string textValue;
        /// <summary>
        /// 文本格式内容
        /// </summary>
        public string TextValue
        {
            get { return this.richTextBox1.Text; }
            set { this.richTextBox1.Text = value; }
        }

        private OpenFileDialog openImageDialog = new OpenFileDialog();

        private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            if (e.ClickedItem.Tag != null)
            {
                switch (e.ClickedItem.Tag.ToString())
                {
                    case "Bold":
                        if (this.richTextBox1.SelectionFont != null)
                        {
                            this.richTextBox1.SelectionFont = new Font(
                                this.richTextBox1.SelectionFont,
                                this.richTextBox1.SelectionFont.Style ^ FontStyle.Bold);
                        }
                        else
                        {
                            this.richTextBox1.SelectionFont = new Font(
                                this.richTextBox1.Font,
                                this.richTextBox1.Font.Style ^ FontStyle.Bold);
                        }
                        break;

                    case "Italic":
                        if (this.richTextBox1.SelectionFont != null)
                        {
                            this.richTextBox1.SelectionFont = new Font(
                                this.richTextBox1.SelectionFont,
                                this.richTextBox1.SelectionFont.Style ^ FontStyle.Italic);
                        }
                        else
                        {
                            this.richTextBox1.SelectionFont = new Font(
                                this.richTextBox1.Font,
                                this.richTextBox1.Font.Style ^ FontStyle.Italic);
                        }
                        break;

                    case "Underline":
                        if (this.richTextBox1.SelectionFont != null)
                        {
                            this.richTextBox1.SelectionFont = new Font(
                                this.richTextBox1.SelectionFont,
                                this.richTextBox1.SelectionFont.Style ^ FontStyle.Underline);
                        }
                        else
                        {
                            this.richTextBox1.SelectionFont = new Font(
                                this.richTextBox1.Font,
                                this.richTextBox1.Font.Style ^ FontStyle.Underline);
                        }
                        break;

                    case "Cut":
                        this.richTextBox1.Cut();
                        break;

                    case "Copy":
                        this.richTextBox1.Copy();
                        break;

                    case "Paste":
                        this.richTextBox1.Paste();
                        break;

                    case "InsertImg":
                        if (this.openImageDialog.ShowDialog() == DialogResult.OK)
                        {
                            string fileName = this.openImageDialog.FileName;
                            try
                            {
                                Bitmap data = new Bitmap(fileName);
                                Clipboard.SetDataObject(data);
                                DataFormats.Format clipFormat = DataFormats.GetFormat(DataFormats.Bitmap);
                                if (this.richTextBox1.CanPaste(clipFormat))
                                {
                                    this.richTextBox1.Paste(clipFormat);
                                }
                            }
                            catch (Exception exception)
                            {
                                MessageBox.Show("图片插入失败:" + exception.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                            }
                        }
                        break;

                    case "Color":
                        if (this.colorDialog1.ShowDialog() == DialogResult.OK)
                        {
                            this.richTextBox1.SelectionColor = this.colorDialog1.Color;
                        }
                        break;

                    case "Undo":
                        this.richTextBox1.Undo();
                        break;

                    case "Redo":
                        this.richTextBox1.Redo();
                        break;

                    case "Left":
                        this.richTextBox1.SelectionAlignment = HorizontalAlignment.Left;
                        break;

                    case "Center":
                        this.richTextBox1.SelectionAlignment = HorizontalAlignment.Center;
                        break;

                    case "Right":
                        this.richTextBox1.SelectionAlignment = HorizontalAlignment.Right;
                        break;

                    default:
                        break;
                }
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }
    }
}Code language: PHP (php)

Tag 标识与功能对照

Tag 值功能
Bold加粗/取消加粗
Italic斜体/取消斜体
Underline下划线/取消下划线
Cut剪切
Copy复制
Paste粘贴
InsertImg插入图片
Color设置字体颜色
Undo撤销
Redo重做
Left左对齐
Center居中对齐
Right右对齐

核心原理说明

  • SelectionFont​ — 获取或设置当前选中文本的字体系列,通过 ^ FontStyle.Bold 等位运算实现切换
  • SelectionColor​ — 配合 ColorDialog 设置选中文本颜色
  • SelectionAlignment​ — 设置段落对齐方式
  • 图片插入​ — 先将图片放入剪贴板,再以 Bitmap 格式粘贴到 RichTextBox 中
  • Rtf 属性​ — 包含完整格式信息,可用于保存和加载带格式的文档

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注