您的位置:首页 > 编程语言 > C#

C# RichTextBox 制作文本编辑器

2017-04-10 23:01 190 查看
本文利用一个简单的小例子【文本编辑器】,讲解RichTextBox的用法,仅供学习分享使用,如有不足之处,还请指正。

Windows窗体中的RichTextBox控件用于显示,输入和操作格式化的文本,RichTextBox除了拥有TextBox控件的所有功能外,还可以显示字体,颜色,链接,从文件中读取和加载图像,以及查找指定的字符。RichTextBox控件通常用于提供类似字体处理程序(如Microsoft Word)的文本操作和显示功能。RichTextBox控件可以显示滚动条,且默认根据需要进行显示。

涉及知识点:

SelectionFont 获取或设置当前选定文本或插入点的字体。

FontStyle 指定应用到文本的字形信息。

SelectionAlignment 获取或设置应用到当前选定内容或插入点的对齐方式。

SelectionIndent 获取或设置所选内容开始行的缩进距离(以像素为单位)。

SelectionCharOffset 获取或设置控件中的文本是显示在基线上、作为上标还是作为基线下方的下标。

SelectionColor 获取或设置当前选定文本或插入点的文本颜色。

SelectionBackColor 获取或设置在 System.Windows.Forms.RichTextBox 控件中选中文本时文本的颜色。

SelectionBullet 获取或设置一个值,通过该值指示项目符号样式是否应用到当前选定内容或插入点。

Clipboard Paste 粘贴指定剪贴板格式的剪贴板内容【插入图片时使用】。

Find 在对搜索应用特定选项的情况下,在 System.Windows.Forms.RichTextBox 控件的文本中搜索位于控件内特定位置的字符串。

效果图

如下【以下设置文本对应的格式】:

1 using DemoRichText.Model;
2 using System;
3 using System.Collections.Generic;
4 using System.ComponentModel;
5 using System.Data;
6 using System.Drawing;
7 using System.Linq;
8 using System.Text;
9 using System.Threading.Tasks;
10 using System.Windows.Forms;
11
12 namespace DemoRichText
13 {
14     public partial class MainForm : Form
15     {
16         public MainForm()
17         {
18             InitializeComponent();
19         }
20
21
22         public void btnButtonClick(object sender, EventArgs e) {
23             Button btn = (Button)sender;
24             BTNType btnType;
25             if (Enum.TryParse<BTNType>(btn.Tag.ToString(), out btnType)) {
26                 if (btnType == BTNType.Search) {
27                     if (!string.IsNullOrEmpty(this.txtSearch.Text.Trim()))
28                     {
29                         this.rtbInfo.Tag = this.txtSearch.Text.Trim();
30                     }
31                     else {
32                         return;
33                     }
34
35                 }
36                 IRichFormat richFomat = RichFormatFactory.CreateRichFormat(btnType);
37                 richFomat.SetFormat(this.rtbInfo);
38             }
39         }
40
41         private void combFontSize_SelectedIndexChanged(object sender, EventArgs e)
42         {
43             float fsize = 12.0f;
44             if (combFontSize.SelectedIndex > -1) {
45                 if (float.TryParse(combFontSize.SelectedItem.ToString(), out fsize)) {
46                     rtbInfo.Tag = fsize.ToString();
47                     IRichFormat richFomat = RichFormatFactory.CreateRichFormat(BTNType.FontSize);
48                     richFomat.SetFormat(this.rtbInfo);
49                 }
50                 return;
51             }
52         }
53     }
54 }


View Code

RichTextBox是一个功能丰富的控件,值得学习。

源码下载链接

源码链接
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: