[示例涉及]:
1、RichTextBox以及相关常见属性
[示例代码]:2文件(其余默认)
Form1.Designer.cs
1
namespace
WA_RichTextBoxTest
2 {
3 partial class Form1
4 {
5 /**//// <summary>
6 /// 必需的设计器变量。
7 /// </summary>
8 private System.ComponentModel.IContainer components = null;
9
10 /**//// <summary>
11 /// 清理所有正在使用的资源。
12 /// </summary>
13 /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
14 protected override void Dispose(bool disposing)
15 {
16 if (disposing && (components != null))
17 {
18 components.Dispose();
19 }
20 base.Dispose(disposing);
21 }
22
23 Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
24
25 /**//// <summary>
26 /// 设计器支持所需的方法 - 不要
27 /// 使用代码编辑器修改此方法的内容。
28 /// </summary>
29 private void InitializeComponent()
30 {
31 this.buttonBold = new System.Windows.Forms.Button();
32 this.buttonUnderline = new System.Windows.Forms.Button();
33 this.buttonItalic = new System.Windows.Forms.Button();
34 this.buttonCenter = new System.Windows.Forms.Button();
35 this.richTextBoxText = new System.Windows.Forms.RichTextBox();
36 this.textBoxSize = new System.Windows.Forms.TextBox();
37 this.labelSize = new System.Windows.Forms.Label();
38 this.buttonLoad = new System.Windows.Forms.Button();
39 this.buttonSave = new System.Windows.Forms.Button();
40 this.SuspendLayout();
41 //
42 // buttonBold
43 //
44 this.buttonBold.Anchor = System.Windows.Forms.AnchorStyles.Top;
45 this.buttonBold.Location = new System.Drawing.Point(12, 12);
46 this.buttonBold.Name = "buttonBold";
47 this.buttonBold.Size = new System.Drawing.Size(116, 23);
48 this.buttonBold.TabIndex = 0;
49 this.buttonBold.Text = "buttonBold";
50 this.buttonBold.UseVisualStyleBackColor = true;
51 this.buttonBold.Click += new System.EventHandler(this.buttonBold_Click);
52 //
53 // buttonUnderline
54 //
55 this.buttonUnderline.Anchor = System.Windows.Forms.AnchorStyles.Top;
56 this.buttonUnderline.Location = new System.Drawing.Point(174, 12);
57 this.buttonUnderline.Name = "buttonUnderline";
58 this.buttonUnderline.Size = new System.Drawing.Size(116, 23);
59 this.buttonUnderline.TabIndex = 1;
60 this.buttonUnderline.Text = "buttonUnderline";
61 this.buttonUnderline.UseVisualStyleBackColor = true;
62 this.buttonUnderline.Click += new System.EventHandler(this.buttonUnderline_Click);
63 //
64 // buttonItalic
65 //
66 this.buttonItalic.Anchor = System.Windows.Forms.AnchorStyles.Top;
67 this.buttonItalic.Location = new System.Drawing.Point(336, 12);
68 this.buttonItalic.Name = "buttonItalic";
69 this.buttonItalic.Size = new System.Drawing.Size(116, 23);
70 this.buttonItalic.TabIndex = 2;
71 this.buttonItalic.Text = "buttonItalic";
72 this.buttonItalic.UseVisualStyleBackColor = true;
73 this.buttonItalic.Click += new System.EventHandler(this.buttonItalic_Click);
74 //
75 // buttonCenter
76 //
77 this.buttonCenter.Anchor = System.Windows.Forms.AnchorStyles.Top;
78 this.buttonCenter.Location = new System.Drawing.Point(498, 12);
79 this.buttonCenter.Name = "buttonCenter";
80 this.buttonCenter.Size = new System.Drawing.Size(116, 23);
81 this.buttonCenter.TabIndex = 3;
82 this.buttonCenter.Text = "buttonCenter";
83 this.buttonCenter.UseVisualStyleBackColor = true;
84 this.buttonCenter.Click += new System.EventHandler(this.buttonCenter_Click);
85 //
86 // richTextBoxText
87 //
88 this.richTextBoxText.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
89 | System.Windows.Forms.AnchorStyles.Left)
90 | System.Windows.Forms.AnchorStyles.Right)));
91 this.richTextBoxText.Location = new System.Drawing.Point(12, 70);
92 this.richTextBoxText.Name = "richTextBoxText";
93 this.richTextBoxText.Size = new System.Drawing.Size(604, 185);
94 this.richTextBoxText.TabIndex = 4;
95 this.richTextBoxText.Text = "";
96 this.richTextBoxText.LinkClicked += new System.Windows.Forms.LinkClickedEventHandler(this.richTextBoxText_LinkClicked);
97 //
98 // textBoxSize
99 //
100 this.textBoxSize.Anchor = System.Windows.Forms.AnchorStyles.Top;
101 this.textBoxSize.Location = new System.Drawing.Point(288, 41);
102 this.textBoxSize.Name = "textBoxSize";
103 this.textBoxSize.Size = new System.Drawing.Size(148, 21);
104 this.textBoxSize.TabIndex = 5;
105 this.textBoxSize.Text = "10";
106 this.textBoxSize.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
107 this.textBoxSize.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBoxSize_KeyPress);
108 this.textBoxSize.Validating += new System.ComponentModel.CancelEventHandler(this.textBoxSize_Validating);
109 //
110 // labelSize
111 //
112 this.labelSize.Anchor = System.Windows.Forms.AnchorStyles.Top;
113 this.labelSize.AutoSize = true;
114 this.labelSize.Location = new System.Drawing.Point(203, 44);
115 this.labelSize.Name = "labelSize";
116 this.labelSize.Size = new System.Drawing.Size(59, 12);
117 this.labelSize.TabIndex = 6;
118 this.labelSize.Text = "labelSize";
119 //
120 // buttonLoad
121 //
122 this.buttonLoad.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
123 this.buttonLoad.Location = new System.Drawing.Point(174, 266);
124 this.buttonLoad.Name = "buttonLoad";
125 this.buttonLoad.Size = new System.Drawing.Size(116, 23);
126 this.buttonLoad.TabIndex = 7;
127 this.buttonLoad.Text = "buttonLoad";
128 this.buttonLoad.UseVisualStyleBackColor = true;
129 this.buttonLoad.Click += new System.EventHandler(this.buttonLoad_Click);
130 //
131 // buttonSave
132 //
133 this.buttonSave.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
134 this.buttonSave.Location = new System.Drawing.Point(336, 266);
135 this.buttonSave.Name = "buttonSave";
136 this.buttonSave.Size = new System.Drawing.Size(116, 23);
137 this.buttonSave.TabIndex = 8;
138 this.buttonSave.Text = "buttonSave";
139 this.buttonSave.UseVisualStyleBackColor = true;
140 this.buttonSave.Click += new System.EventHandler(this.buttonSave_Click);
141 //
142 // Form1
143 //
144 this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
145 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
146 this.ClientSize = new System.Drawing.Size(628, 295);
147 this.Controls.Add(this.buttonSave);
148 this.Controls.Add(this.buttonLoad);
149 this.Controls.Add(this.labelSize);
150 this.Controls.Add(this.textBoxSize);
151 this.Controls.Add(this.richTextBoxText);
152 this.Controls.Add(this.buttonCenter);
153 this.Controls.Add(this.buttonItalic);
154 this.Controls.Add(this.buttonUnderline);
155 this.Controls.Add(this.buttonBold);
156 this.MinimumSize = new System.Drawing.Size(636, 329);
157 this.Name = "Form1";
158 this.Text = "Form1";
159 this.DragDrop += new System.Windows.Forms.DragEventHandler(this.Form1_DragDrop);
160 this.DragEnter += new System.Windows.Forms.DragEventHandler(this.Form1_DragEnter);
161 this.ResumeLayout(false);
162 this.PerformLayout();
163
164 }
165
166 #endregion
167
168 private System.Windows.Forms.Button buttonBold;
169 private System.Windows.Forms.Button buttonUnderline;
170 private System.Windows.Forms.Button buttonItalic;
171 private System.Windows.Forms.Button buttonCenter;
172 private System.Windows.Forms.RichTextBox richTextBoxText;
173 private System.Windows.Forms.TextBox textBoxSize;
174 private System.Windows.Forms.Label labelSize;
175 private System.Windows.Forms.Button buttonLoad;
176 private System.Windows.Forms.Button buttonSave;
177 }
178}
179
180
2 {
3 partial class Form1
4 {
5 /**//// <summary>
6 /// 必需的设计器变量。
7 /// </summary>
8 private System.ComponentModel.IContainer components = null;
9
10 /**//// <summary>
11 /// 清理所有正在使用的资源。
12 /// </summary>
13 /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
14 protected override void Dispose(bool disposing)
15 {
16 if (disposing && (components != null))
17 {
18 components.Dispose();
19 }
20 base.Dispose(disposing);
21 }
22
23 Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
24
25 /**//// <summary>
26 /// 设计器支持所需的方法 - 不要
27 /// 使用代码编辑器修改此方法的内容。
28 /// </summary>
29 private void InitializeComponent()
30 {
31 this.buttonBold = new System.Windows.Forms.Button();
32 this.buttonUnderline = new System.Windows.Forms.Button();
33 this.buttonItalic = new System.Windows.Forms.Button();
34 this.buttonCenter = new System.Windows.Forms.Button();
35 this.richTextBoxText = new System.Windows.Forms.RichTextBox();
36 this.textBoxSize = new System.Windows.Forms.TextBox();
37 this.labelSize = new System.Windows.Forms.Label();
38 this.buttonLoad = new System.Windows.Forms.Button();
39 this.buttonSave = new System.Windows.Forms.Button();
40 this.SuspendLayout();
41 //
42 // buttonBold
43 //
44 this.buttonBold.Anchor = System.Windows.Forms.AnchorStyles.Top;
45 this.buttonBold.Location = new System.Drawing.Point(12, 12);
46 this.buttonBold.Name = "buttonBold";
47 this.buttonBold.Size = new System.Drawing.Size(116, 23);
48 this.buttonBold.TabIndex = 0;
49 this.buttonBold.Text = "buttonBold";
50 this.buttonBold.UseVisualStyleBackColor = true;
51 this.buttonBold.Click += new System.EventHandler(this.buttonBold_Click);
52 //
53 // buttonUnderline
54 //
55 this.buttonUnderline.Anchor = System.Windows.Forms.AnchorStyles.Top;
56 this.buttonUnderline.Location = new System.Drawing.Point(174, 12);
57 this.buttonUnderline.Name = "buttonUnderline";
58 this.buttonUnderline.Size = new System.Drawing.Size(116, 23);
59 this.buttonUnderline.TabIndex = 1;
60 this.buttonUnderline.Text = "buttonUnderline";
61 this.buttonUnderline.UseVisualStyleBackColor = true;
62 this.buttonUnderline.Click += new System.EventHandler(this.buttonUnderline_Click);
63 //
64 // buttonItalic
65 //
66 this.buttonItalic.Anchor = System.Windows.Forms.AnchorStyles.Top;
67 this.buttonItalic.Location = new System.Drawing.Point(336, 12);
68 this.buttonItalic.Name = "buttonItalic";
69 this.buttonItalic.Size = new System.Drawing.Size(116, 23);
70 this.buttonItalic.TabIndex = 2;
71 this.buttonItalic.Text = "buttonItalic";
72 this.buttonItalic.UseVisualStyleBackColor = true;
73 this.buttonItalic.Click += new System.EventHandler(this.buttonItalic_Click);
74 //
75 // buttonCenter
76 //
77 this.buttonCenter.Anchor = System.Windows.Forms.AnchorStyles.Top;
78 this.buttonCenter.Location = new System.Drawing.Point(498, 12);
79 this.buttonCenter.Name = "buttonCenter";
80 this.buttonCenter.Size = new System.Drawing.Size(116, 23);
81 this.buttonCenter.TabIndex = 3;
82 this.buttonCenter.Text = "buttonCenter";
83 this.buttonCenter.UseVisualStyleBackColor = true;
84 this.buttonCenter.Click += new System.EventHandler(this.buttonCenter_Click);
85 //
86 // richTextBoxText
87 //
88 this.richTextBoxText.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
89 | System.Windows.Forms.AnchorStyles.Left)
90 | System.Windows.Forms.AnchorStyles.Right)));
91 this.richTextBoxText.Location = new System.Drawing.Point(12, 70);
92 this.richTextBoxText.Name = "richTextBoxText";
93 this.richTextBoxText.Size = new System.Drawing.Size(604, 185);
94 this.richTextBoxText.TabIndex = 4;
95 this.richTextBoxText.Text = "";
96 this.richTextBoxText.LinkClicked += new System.Windows.Forms.LinkClickedEventHandler(this.richTextBoxText_LinkClicked);
97 //
98 // textBoxSize
99 //
100 this.textBoxSize.Anchor = System.Windows.Forms.AnchorStyles.Top;
101 this.textBoxSize.Location = new System.Drawing.Point(288, 41);
102 this.textBoxSize.Name = "textBoxSize";
103 this.textBoxSize.Size = new System.Drawing.Size(148, 21);
104 this.textBoxSize.TabIndex = 5;
105 this.textBoxSize.Text = "10";
106 this.textBoxSize.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
107 this.textBoxSize.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBoxSize_KeyPress);
108 this.textBoxSize.Validating += new System.ComponentModel.CancelEventHandler(this.textBoxSize_Validating);
109 //
110 // labelSize
111 //
112 this.labelSize.Anchor = System.Windows.Forms.AnchorStyles.Top;
113 this.labelSize.AutoSize = true;
114 this.labelSize.Location = new System.Drawing.Point(203, 44);
115 this.labelSize.Name = "labelSize";
116 this.labelSize.Size = new System.Drawing.Size(59, 12);
117 this.labelSize.TabIndex = 6;
118 this.labelSize.Text = "labelSize";
119 //
120 // buttonLoad
121 //
122 this.buttonLoad.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
123 this.buttonLoad.Location = new System.Drawing.Point(174, 266);
124 this.buttonLoad.Name = "buttonLoad";
125 this.buttonLoad.Size = new System.Drawing.Size(116, 23);
126 this.buttonLoad.TabIndex = 7;
127 this.buttonLoad.Text = "buttonLoad";
128 this.buttonLoad.UseVisualStyleBackColor = true;
129 this.buttonLoad.Click += new System.EventHandler(this.buttonLoad_Click);
130 //
131 // buttonSave
132 //
133 this.buttonSave.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
134 this.buttonSave.Location = new System.Drawing.Point(336, 266);
135 this.buttonSave.Name = "buttonSave";
136 this.buttonSave.Size = new System.Drawing.Size(116, 23);
137 this.buttonSave.TabIndex = 8;
138 this.buttonSave.Text = "buttonSave";
139 this.buttonSave.UseVisualStyleBackColor = true;
140 this.buttonSave.Click += new System.EventHandler(this.buttonSave_Click);
141 //
142 // Form1
143 //
144 this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
145 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
146 this.ClientSize = new System.Drawing.Size(628, 295);
147 this.Controls.Add(this.buttonSave);
148 this.Controls.Add(this.buttonLoad);
149 this.Controls.Add(this.labelSize);
150 this.Controls.Add(this.textBoxSize);
151 this.Controls.Add(this.richTextBoxText);
152 this.Controls.Add(this.buttonCenter);
153 this.Controls.Add(this.buttonItalic);
154 this.Controls.Add(this.buttonUnderline);
155 this.Controls.Add(this.buttonBold);
156 this.MinimumSize = new System.Drawing.Size(636, 329);
157 this.Name = "Form1";
158 this.Text = "Form1";
159 this.DragDrop += new System.Windows.Forms.DragEventHandler(this.Form1_DragDrop);
160 this.DragEnter += new System.Windows.Forms.DragEventHandler(this.Form1_DragEnter);
161 this.ResumeLayout(false);
162 this.PerformLayout();
163
164 }
165
166 #endregion
167
168 private System.Windows.Forms.Button buttonBold;
169 private System.Windows.Forms.Button buttonUnderline;
170 private System.Windows.Forms.Button buttonItalic;
171 private System.Windows.Forms.Button buttonCenter;
172 private System.Windows.Forms.RichTextBox richTextBoxText;
173 private System.Windows.Forms.TextBox textBoxSize;
174 private System.Windows.Forms.Label labelSize;
175 private System.Windows.Forms.Button buttonLoad;
176 private System.Windows.Forms.Button buttonSave;
177 }
178}
179
180
Form1.cs
1
using
System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Text;
7 using System.Windows.Forms;
8
9 namespace WA_RichTextBoxTest
10 {
11 public partial class Form1 : Form
12 {
13 public Form1()
14 {
15 InitializeComponent();
16 }
17
18 private void buttonBold_Click(object sender, EventArgs e)
19 {
20 Font oldFont;
21 Font newFont;
22
23 //获取选定字的字体
24 oldFont = this.richTextBoxText.SelectionFont;
25
26 //如果字体是粗体,我们就将它的粗体格式移除
27 if (oldFont.Bold)
28 newFont = new Font(oldFont, oldFont.Style & ~FontStyle.Bold);
29 else
30 newFont = new Font(oldFont, oldFont.Style | FontStyle.Bold);
31
32 //插入新字体并对RichTextBoxText设置输入焦点
33 this.richTextBoxText.SelectionFont = newFont;
34 this.richTextBoxText.Focus();
35 }
36
37 private void buttonUnderline_Click(object sender, EventArgs e)
38 {
39 Font oldFont;
40 Font newFont;
41
42 //获取选定字的字体
43 oldFont = this.richTextBoxText.SelectionFont;
44
45 //如果有下划线,我们就将它的下划线格式移除
46 if (oldFont.Underline)
47 newFont = new Font(oldFont, oldFont.Style & ~FontStyle.Underline);
48 else
49 newFont = new Font(oldFont, oldFont.Style | FontStyle.Underline);
50
51 //插入新字体并对RichTextBoxText设置输入焦点
52 this.richTextBoxText.SelectionFont = newFont;
53 this.richTextBoxText.Focus();
54 }
55
56 private void buttonItalic_Click(object sender, EventArgs e)
57 {
58 Font oldFont;
59 Font newFont;
60
61 //获取选定字的字体
62 oldFont = this.richTextBoxText.SelectionFont;
63
64 //如果字体是斜体,我们就将它的斜体格式移除
65 if (oldFont.Italic)
66 newFont = new Font(oldFont, oldFont.Style & ~FontStyle.Italic);
67 else
68 newFont = new Font(oldFont, oldFont.Style | FontStyle.Italic);
69
70 //插入新字体并对RichTextBoxText设置输入焦点
71 this.richTextBoxText.SelectionFont = newFont;
72 this.richTextBoxText.Focus();
73 }
74
75 private void buttonCenter_Click(object sender, EventArgs e)
76 {
77 if (this.richTextBoxText.SelectionAlignment == HorizontalAlignment.Center)
78 this.richTextBoxText.SelectionAlignment = HorizontalAlignment.Left;
79 else
80 this.richTextBoxText.SelectionAlignment = HorizontalAlignment.Center;
81
82 this.richTextBoxText.Focus();
83 }
84
85 private void textBoxSize_KeyPress(object sender, KeyPressEventArgs e)
86 {
87 //移除所有的非数字,退格,回车字符
88 if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != 13)
89 e.Handled = true;
90 else if (e.KeyChar == 13)
91 {
92 //如果用户键入回车就设置字号
93 TextBox txt = (TextBox)sender;
94
95 if (txt.Text.Length > 0)
96 ApplyTextSize(txt.Text);
97 e.Handled = true;
98 this.richTextBoxText.Focus();
99 }
100 }
101
102 private void textBoxSize_Validating(object sender, CancelEventArgs e)
103 {
104 TextBox txt = (TextBox)sender;
105 ApplyTextSize(txt.Text);
106 this.richTextBoxText.Focus();
107 }
108
109 private void ApplyTextSize(string textSize)
110 {
111 //因为我们将需要一个浮点型,所以我们转换text到float
112 float newSize = Convert.ToSingle(textSize);
113 FontFamily currentFontFamily;
114 Font newFont;
115
116 //创建一个新的字体其他的family属性相同,除了新的尺寸
117 currentFontFamily = this.richTextBoxText.SelectionFont.FontFamily;
118 newFont = new Font(currentFontFamily, newSize);
119
120 //设置选择文字的字号
121 this.richTextBoxText.SelectionFont = newFont;
122 }
123
124 private void richTextBoxText_LinkClicked(object sender, LinkClickedEventArgs e)
125 {
126 System.Diagnostics.Process.Start(e.LinkText);
127 }
128
129 private void buttonLoad_Click(object sender, EventArgs e)
130 {
131 //将文件载入到RichTextBox中
132 try
133 {
134 richTextBoxText.LoadFile("Test.rtf");
135 }
136 catch (System.IO.FileNotFoundException)
137 {
138 MessageBox.Show("文件不存在!");
139 }
140 }
141
142 private void buttonSave_Click(object sender, EventArgs e)
143 {
144 //保存文件
145 try
146 {
147 richTextBoxText.SaveFile("Test.rtf");
148 }
149 catch (System.Exception err)
150 {
151 MessageBox.Show(err.Message);
152 }
153 }
154
155 private void Form1_DragDrop(object sender, DragEventArgs e)
156 {
157 buttonSave_Click(sender, e);
158 }
159
160 private void Form1_DragEnter(object sender, DragEventArgs e)
161 {
162 this.Focus();
163 }
164 }
165}
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Text;
7 using System.Windows.Forms;
8
9 namespace WA_RichTextBoxTest
10 {
11 public partial class Form1 : Form
12 {
13 public Form1()
14 {
15 InitializeComponent();
16 }
17
18 private void buttonBold_Click(object sender, EventArgs e)
19 {
20 Font oldFont;
21 Font newFont;
22
23 //获取选定字的字体
24 oldFont = this.richTextBoxText.SelectionFont;
25
26 //如果字体是粗体,我们就将它的粗体格式移除
27 if (oldFont.Bold)
28 newFont = new Font(oldFont, oldFont.Style & ~FontStyle.Bold);
29 else
30 newFont = new Font(oldFont, oldFont.Style | FontStyle.Bold);
31
32 //插入新字体并对RichTextBoxText设置输入焦点
33 this.richTextBoxText.SelectionFont = newFont;
34 this.richTextBoxText.Focus();
35 }
36
37 private void buttonUnderline_Click(object sender, EventArgs e)
38 {
39 Font oldFont;
40 Font newFont;
41
42 //获取选定字的字体
43 oldFont = this.richTextBoxText.SelectionFont;
44
45 //如果有下划线,我们就将它的下划线格式移除
46 if (oldFont.Underline)
47 newFont = new Font(oldFont, oldFont.Style & ~FontStyle.Underline);
48 else
49 newFont = new Font(oldFont, oldFont.Style | FontStyle.Underline);
50
51 //插入新字体并对RichTextBoxText设置输入焦点
52 this.richTextBoxText.SelectionFont = newFont;
53 this.richTextBoxText.Focus();
54 }
55
56 private void buttonItalic_Click(object sender, EventArgs e)
57 {
58 Font oldFont;
59 Font newFont;
60
61 //获取选定字的字体
62 oldFont = this.richTextBoxText.SelectionFont;
63
64 //如果字体是斜体,我们就将它的斜体格式移除
65 if (oldFont.Italic)
66 newFont = new Font(oldFont, oldFont.Style & ~FontStyle.Italic);
67 else
68 newFont = new Font(oldFont, oldFont.Style | FontStyle.Italic);
69
70 //插入新字体并对RichTextBoxText设置输入焦点
71 this.richTextBoxText.SelectionFont = newFont;
72 this.richTextBoxText.Focus();
73 }
74
75 private void buttonCenter_Click(object sender, EventArgs e)
76 {
77 if (this.richTextBoxText.SelectionAlignment == HorizontalAlignment.Center)
78 this.richTextBoxText.SelectionAlignment = HorizontalAlignment.Left;
79 else
80 this.richTextBoxText.SelectionAlignment = HorizontalAlignment.Center;
81
82 this.richTextBoxText.Focus();
83 }
84
85 private void textBoxSize_KeyPress(object sender, KeyPressEventArgs e)
86 {
87 //移除所有的非数字,退格,回车字符
88 if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != 13)
89 e.Handled = true;
90 else if (e.KeyChar == 13)
91 {
92 //如果用户键入回车就设置字号
93 TextBox txt = (TextBox)sender;
94
95 if (txt.Text.Length > 0)
96 ApplyTextSize(txt.Text);
97 e.Handled = true;
98 this.richTextBoxText.Focus();
99 }
100 }
101
102 private void textBoxSize_Validating(object sender, CancelEventArgs e)
103 {
104 TextBox txt = (TextBox)sender;
105 ApplyTextSize(txt.Text);
106 this.richTextBoxText.Focus();
107 }
108
109 private void ApplyTextSize(string textSize)
110 {
111 //因为我们将需要一个浮点型,所以我们转换text到float
112 float newSize = Convert.ToSingle(textSize);
113 FontFamily currentFontFamily;
114 Font newFont;
115
116 //创建一个新的字体其他的family属性相同,除了新的尺寸
117 currentFontFamily = this.richTextBoxText.SelectionFont.FontFamily;
118 newFont = new Font(currentFontFamily, newSize);
119
120 //设置选择文字的字号
121 this.richTextBoxText.SelectionFont = newFont;
122 }
123
124 private void richTextBoxText_LinkClicked(object sender, LinkClickedEventArgs e)
125 {
126 System.Diagnostics.Process.Start(e.LinkText);
127 }
128
129 private void buttonLoad_Click(object sender, EventArgs e)
130 {
131 //将文件载入到RichTextBox中
132 try
133 {
134 richTextBoxText.LoadFile("Test.rtf");
135 }
136 catch (System.IO.FileNotFoundException)
137 {
138 MessageBox.Show("文件不存在!");
139 }
140 }
141
142 private void buttonSave_Click(object sender, EventArgs e)
143 {
144 //保存文件
145 try
146 {
147 richTextBoxText.SaveFile("Test.rtf");
148 }
149 catch (System.Exception err)
150 {
151 MessageBox.Show(err.Message);
152 }
153 }
154
155 private void Form1_DragDrop(object sender, DragEventArgs e)
156 {
157 buttonSave_Click(sender, e);
158 }
159
160 private void Form1_DragEnter(object sender, DragEventArgs e)
161 {
162 this.Focus();
163 }
164 }
165}
[示例说明]:
1、开发语言:C#
2、开发环境:Visual Studio.Net 2005 Team suite
3、开发模板:C#.net项目->Windows应用程序