formrichtextbox.cs

来自「csharp课本的源代码」· CS 代码 · 共 104 行

CS
104
字号
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace RichTextBoxExample
{
    public partial class FormRichTextBox : Form
    {
        public FormRichTextBox()
        {
            InitializeComponent();
        }

        private void buttonDisplay_Click(object sender, EventArgs e)
        {
            richTextBox1.Clear();
            richTextBox1.SelectedText = "窗体使用介绍:" + "\n";
            richTextBox1.SelectionBullet = true;
            richTextBox1.SelectionFont = new Font("隶书", 14, System.Drawing.FontStyle.Bold);
            richTextBox1.SelectionColor = Color.OrangeRed;
            richTextBox1.SelectedText = "读入的RTF文件为:source.rtf\n";
            richTextBox1.SelectionFont = new Font("宋体", 14, System.Drawing.FontStyle.Bold);
            richTextBox1.SelectionColor = Color.LightSalmon;
            richTextBox1.SelectedText = "保存RTF文件为:result.rtf\n";
            richTextBox1.SelectionFont = new Font("隶书", 14, System.Drawing.FontStyle.Bold);
            richTextBox1.SelectionColor = Color.BlueViolet;
            richTextBox1.SelectedText =
               "可以使用锁定功能保护文本中的关键字符,以免被错误地修改\n";
            richTextBox1.SelectionFont = new Font("隶书", 14, System.Drawing.FontStyle.Bold);
            richTextBox1.SelectionColor = Color.Purple;
            richTextBox1.SelectedText = "可以使用替换功能修改文本中的某些内容,或直接修改。\n";
            richTextBox1.SelectionBullet = false;

        }

        private void buttonOpenFile_Click(object sender, EventArgs e)
        {
            richTextBox1.LoadFile("D:\\source.rtf", RichTextBoxStreamType.RichText);
        }

        private void buttonSaveFile_Click(object sender, EventArgs e)
        {
            richTextBox1.SaveFile("D:\\result.rtf", RichTextBoxStreamType.RichText);
            MessageBox.Show("当前文本已保存于“result.rtf”文件中!");

        }

        private void buttonProtected_Click(object sender, EventArgs e)
        {
            int myText2 = richTextBox1.Find(textBoxProtected.Text);
            if (textBoxProtected.Text == "")
            {
                MessageBox.Show("没有指定要保护的信息!");
                return;
            }
            if (myText2 == -1)
            {
                MessageBox.Show("文本中不存在指定的字符串<" + textBoxProtected.Text + ">!");
                return;
            }
            richTextBox1.SelectionProtected = true;

        }

        private void buttonFind_Click(object sender, EventArgs e)
        {
            //若没有指定替换或被替换字符串,则不应执行该事件
            if (textBoxFind1.Text == "" || textBoxFind2.Text == "")
            {
                MessageBox.Show("没有完整指定替换或被替换字符串!");
                return;
            }
            //若没有找到所要替换的指定字符串,则提示并退出
            int myTextPosition = richTextBox1.Find(textBoxFind1.Text);
            if (myTextPosition == -1)
            {
                MessageBox.Show("文本中没有找到字符串<" + textBoxFind1.Text + ">!");
                return;
            }
            //若所要替换的指定字符串是已经被保护的关键信息,则提示不能替换
            if (richTextBox1.SelectionProtected)
            {
                MessageBox.Show("字符串<" +
                    textBoxFind1.Text + ">已经被设为只读状态,不能替换!");
                return;
            }
            //若替换操作的条件合法,则进行替换操作,并给出提示信息
            richTextBox1.SelectionColor = Color.Red;
            richTextBox1.SelectionFont = new Font("隶书", 16, System.Drawing.FontStyle.Bold);
            richTextBox1.SelectedText = textBoxFind2.Text;
            myTextPosition++;
            labelResult.Visible = true;
            labelResult.Text = string.Format(
                "目前共有{0}个文字,您要查找的<{1}>在第{2}个字符的位置上,已被替换为<{3}>!",
                richTextBox1.Text.Length, textBoxFind1.Text,
                myTextPosition, textBoxFind2.Text);

        }
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?