[示例涉及]:
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 窗体设计器生成的代码
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

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

167

168

169

170

171

172

173

174

175

176

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

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

[示例说明]:
1、开发语言:C#
2、开发环境:Visual Studio.Net 2005 Team suite
3、开发模板:C#.net项目->Windows应用程序