winForm中RichTextBox富文本框的使用

创建一个RichTextBox控件

  • 属性
    a. 行为-EnableAutoDragDrop:True 允许拖拽
  1. 打开文件
    先添加一个button按钮,注册单击事件表示打开某个文件
 private void open_Click(object sender, EventArgs e)
        {
            //创建一个打开文件对话框
            OpenFileDialog openFile1 = new OpenFileDialog();
            //限定打开文件的初始化文件目录
            openFile1.InitialDirectory = "D:\\CSharp";
            //筛选打开文件的类型为RTF格式
            openFile1.DefaultExt = "*.rtf";
            openFile1.Filter = "RTF Files|*.rtf";
            //判断用户是否从打开文件对话框中选择了一个文件
            if(openFile1.ShowDialog() == System.Windows.Forms.DialogResult.OK&&
                openFile1.FileName.Length>0)
            {
                myNote.LoadFile(openFile1.FileName);
            }
            //用ANSI编码格式保存txt文本避免出现乱码
            //myNote.LoadFile("D:\\CSharp\\notes\\test.txt", RichTextBoxStreamType.PlainText);
            //myNote.LoadFile("D:\\CSharp\\notes\\test.rtf", RichTextBoxStreamType.RichText);
        }

对于txt文件,如果不是ANSI编码格式的如何解决?
2. 保存文件
同样添加一个button按钮,表示保存按钮,注册单击事件

private void save_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFile1 = new SaveFileDialog();
            saveFile1.InitialDirectory = "D:\\CSharp";
            saveFile1.DefaultExt = "*.rtf";
            saveFile1.Filter = "RTF Files|*.rtf";
            if(saveFile1.ShowDialog() ==System.Windows.Forms.DialogResult.OK
                &&saveFile1.FileName.Length>0)
            {
                myNote.SaveFile(saveFile1.FileName);
            }
            //myNote.SaveFile("D:\\CSharp\\notes\\test.rtf");
        }

  1. 链接跳转
    注册RichTextBox的LinkClicked事件
private void myNote_LinkClicked(object sender, LinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start(e.LinkText);
        }
  1. 右键复制、粘贴
    a. 创建一个contextMenuScrip,添加项:复制、粘贴
    b. 选中复制:注册其Click事件
    代码:
 private void copy_Click(object sender, EventArgs e)
        {
            Clipboard.SetText(myNote.SelectedText);
        }

选中粘贴,注册其Click事件

 private void paste_Click(object sender, EventArgs e)
        {
            myNote.Paste();
        }

c. 将该contextMenuScrip绑定到富文本框

  1. 插入图片
    创建button
    代码:
private void image_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.DefaultExt = "*.jpg";
            openFileDialog1.Filter = "Image Files|*.jpg";
            if(openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK&&
                openFileDialog1.FileName.Length>0)
            {
                Image insertImage = Image.FromFile(openFileDialog1.FileName);
                Clipboard.SetImage(insertImage);
                myNote.Paste();
            }
        }
  1. 撤销与重做
    创建button
 //撤销
        private void unDo_Click(object sender, EventArgs e)
        {
            myNote.Undo();
        }
        //重做
        private void reDo_Click(object sender, EventArgs e)
        {
            myNote.Redo();
        }
  1. 清空(含清空确认按钮)
    创建button
 private void clear_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("是否要清空MyNote中的所有内容?","清空确认", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if(result == DialogResult.Yes)
            {
                myNote.Clear();
            }
        }
  1. 设计器界面
    winForm中RichTextBox富文本框的使用
  2. 完整代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace study_03
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //打开
        private void open_Click(object sender, EventArgs e)
        {
            //创建一个打开文件对话框
            OpenFileDialog openFile1 = new OpenFileDialog();
            //限定打开文件的初始化文件目录
            openFile1.InitialDirectory = "D:\\CSharp";
            //筛选打开文件的类型为RTF格式
            openFile1.DefaultExt = "*.rtf";
            openFile1.Filter = "RTF Files|*.rtf";
            //判断用户是否从打开文件对话框中选择了一个文件
            if(openFile1.ShowDialog() == System.Windows.Forms.DialogResult.OK&&
                openFile1.FileName.Length>0)
            {
                myNote.LoadFile(openFile1.FileName);
            }
            //用ANSI编码格式保存txt文本避免出现乱码
            //myNote.LoadFile("D:\\CSharp\\notes\\test.txt", RichTextBoxStreamType.PlainText);
            //myNote.LoadFile("D:\\CSharp\\notes\\test.rtf", RichTextBoxStreamType.RichText);
        }
        //另存为
        private void save_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFile1 = new SaveFileDialog();
            saveFile1.InitialDirectory = "D:\\CSharp";
            saveFile1.DefaultExt = "*.rtf";
            saveFile1.Filter = "RTF Files|*.rtf";
            if(saveFile1.ShowDialog() ==System.Windows.Forms.DialogResult.OK
                &&saveFile1.FileName.Length>0)
            {
                myNote.SaveFile(saveFile1.FileName);
            }
            //myNote.SaveFile("D:\\CSharp\\notes\\test.rtf");
        }
        //注册链接跳转
        private void myNote_LinkClicked(object sender, LinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start(e.LinkText);
        }
        //右键复制
        private void copy_Click(object sender, EventArgs e)
        {
            Clipboard.SetText(myNote.SelectedText);
        }
        //右键粘贴
        private void paste_Click(object sender, EventArgs e)
        {
            myNote.Paste();
        }
        //清空
        private void clear_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("是否要清空MyNote中的所有内容?","清空确认", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if(result == DialogResult.Yes)
            {
                myNote.Clear();
            }
        }
        //插入图片
        private void image_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.DefaultExt = "*.jpg";
            openFileDialog1.Filter = "Image Files|*.jpg";
            if(openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK&&
                openFileDialog1.FileName.Length>0)
            {
                Image insertImage = Image.FromFile(openFileDialog1.FileName);
                Clipboard.SetImage(insertImage);
                myNote.Paste();
            }
        }
        //撤销
        private void unDo_Click(object sender, EventArgs e)
        {
            myNote.Undo();
        }
        //重做
        private void reDo_Click(object sender, EventArgs e)
        {
            myNote.Redo();
        }
    }
}

上一篇:SysUtils.StrLCat


下一篇:个人项目开发