为完成《Windows Forms编程实战》第175页的“尝试一下”,特制ContextMenuStrip,有BUG请指评,谢谢!
使用: 可直接new一个,在参数中填TextBox类实例即可。

Code在此
1
public class MyContextMenuStrip : System.Windows.Forms.ContextMenuStrip
2
{
3
/**//// <summary>
4
/// 剪切按钮
5
/// </summary>
6
System.Windows.Forms.ToolStripMenuItem TSMI_Cut =
7
new System.Windows.Forms.ToolStripMenuItem();
8
9
/**//// <summary>
10
/// 复制按钮
11
/// </summary>
12
System.Windows.Forms.ToolStripMenuItem TSMI_Copy =
13
new System.Windows.Forms.ToolStripMenuItem();
14
15
/**//// <summary>
16
/// 粘贴按钮
17
/// </summary>
18
System.Windows.Forms.ToolStripMenuItem TSMI_Paste =
19
new System.Windows.Forms.ToolStripMenuItem();
20
21
/**//// <summary>
22
/// 删除按钮
23
/// </summary>
24
System.Windows.Forms.ToolStripMenuItem TSMI_Delete =
25
new System.Windows.Forms.ToolStripMenuItem();
26
27
/**//// <summary>
28
/// 撤销按钮
29
/// </summary>
30
System.Windows.Forms.ToolStripMenuItem TSMI_Undo =
31
new System.Windows.Forms.ToolStripMenuItem();
32
33
/**//// <summary>
34
/// 全选按钮
35
/// </summary>
36
System.Windows.Forms.ToolStripMenuItem TSMI_SelectAll =
37
new System.Windows.Forms.ToolStripMenuItem();
38
39
/**//// <summary>
40
/// 上分隔条
41
/// </summary>
42
System.Windows.Forms.ToolStripSeparator TSS_Up =
43
new System.Windows.Forms.ToolStripSeparator();
44
45
/**//// <summary>
46
/// 下分隔条
47
/// </summary>
48
System.Windows.Forms.ToolStripSeparator TSS_Down =
49
new System.Windows.Forms.ToolStripSeparator();
50
51
/**//// <summary>
52
/// 构造函数
53
/// </summary>
54
protected MyContextMenuStrip()
55
{
56
this.InitializeComponent();
57
58
this.Items.AddRange(new System.Windows.Forms.ToolStripItem[]
{
59
this.TSMI_Undo,
60
this.TSS_Up,
61
this.TSMI_Cut,
62
this.TSMI_Copy,
63
this.TSMI_Paste,
64
this.TSMI_Delete,
65
this.TSS_Down,
66
this.TSMI_SelectAll
67
});
68
69
this.InitItemEnableInMyCMS();
70
}
71
72
/**//// <summary>
73
/// 构造函数
74
/// </summary>
75
/// <param name="myControl">TextBoxBase类实例</param>
76
public MyContextMenuStrip(System.Windows.Forms.TextBoxBase myControl)
77
: this()
78
{
79
this.myCMS_Object = myControl;
80
81
this.myCMS_Object.MouseDown +=
82
new System.Windows.Forms.MouseEventHandler(this.myCMS_Object_MouseDown);
83
84
this.myCMS_Object.Click +=
85
new System.EventHandler(this.myCMS_Object_Click);
86
87
this.Opened +=
88
new System.EventHandler(this.myCMS_Opened);
89
}
90
91
/**//// <summary>
92
/// TextBoxBase实例字段
93
/// </summary>
94
private System.Windows.Forms.TextBoxBase myCMS_Object;
95
96
/**//// <summary>
97
/// TextBoxBase实例属性
98
/// </summary>
99
public System.Windows.Forms.TextBoxBase MyCMS_Object
100
{
101
get
{ return myCMS_Object; }
102
set
{ myCMS_Object = value; }
103
}
104
105
/**//// <summary>
106
///
107
/// </summary>
108
private int Integer = 0;
109
110
/**//// <summary>
111
/// 初始化上下文菜单项
112
/// </summary>
113
private void InitializeComponent()
114
{
115
//
116
// TSMI_Undo
117
//
118
this.TSMI_Undo.Name = "TSMI_Undo";
119
this.TSMI_Undo.Size = new System.Drawing.Size(152, 22);
120
this.TSMI_Undo.Text = "撤销";
121
this.TSMI_Undo.Click += new EventHandler(this.TSMI_Undo_Click);
122
//
123
// TSS_Up
124
//
125
this.TSS_Up.Name = "TSS_Up";
126
this.TSS_Up.Size = new System.Drawing.Size(152, 22);
127
//
128
// TSS_Down
129
//
130
this.TSS_Down.Name = "TSS_Down";
131
this.TSS_Down.Size = new System.Drawing.Size(152, 22);
132
//
133
// TSMI_Cut
134
//
135
this.TSMI_Cut.Name = "TSMI_Cut";
136
this.TSMI_Cut.Size = new System.Drawing.Size(152, 22);
137
this.TSMI_Cut.Text = "剪切";
138
this.TSMI_Cut.Click += new EventHandler(this.TSMI_Cut_Click);
139
//
140
// TSMI_Copy
141
//
142
this.TSMI_Copy.Name = "TSMI_Copy";
143
this.TSMI_Copy.Size = new System.Drawing.Size(152, 22);
144
this.TSMI_Copy.Text = "复制";
145
this.TSMI_Copy.Click += new EventHandler(TSMI_Copy_Click);
146
//
147
// TSMI_Paste
148
//
149
this.TSMI_Paste.Name = "TSMI_Stick";
150
this.TSMI_Paste.Size = new System.Drawing.Size(152, 22);
151
this.TSMI_Paste.Text = "粘贴";
152
this.TSMI_Paste.Click += new EventHandler(this.TSMI_Paste_Click);
153
//
154
// TSMI_SelectAll
155
//
156
this.TSMI_SelectAll.Name = "TSMI_SelectAll";
157
this.TSMI_SelectAll.Size = new System.Drawing.Size(152, 22);
158
this.TSMI_SelectAll.Text = "全选";
159
this.TSMI_SelectAll.Click += new EventHandler(this.TSMI_SelectAll_Click);
160
//
161
// TSMI_Delete
162
//
163
this.TSMI_Delete.Name = "TSMI_Delete";
164
this.TSMI_Delete.Size = new System.Drawing.Size(152, 22);
165
this.TSMI_Delete.Text = "删除";
166
this.TSMI_Delete.Click += new EventHandler(this.TSMI_Delete_Click);
167
}
168
169
/**//// <summary>
170
/// 撤销
171
/// </summary>
172
/// <param name="sender"></param>
173
/// <param name="e"></param>
174
private void TSMI_Undo_Click(object sender, EventArgs e)
175
{
176
// Determine if last operation can be undone in text box.
177
if (this.myCMS_Object.CanUndo == true)
178
{
179
// Undo the last operation.
180
this.myCMS_Object.Undo();
181
// Clear the undo buffer to prevent last action from being redone.
182
this.myCMS_Object.ClearUndo();
183
}
184
185
}
186
187
/**//// <summary>
188
/// 剪切
189
/// </summary>
190
/// <param name="sender"></param>
191
/// <param name="e"></param>
192
private void TSMI_Cut_Click(object sender, EventArgs e)
193
{
194
// Ensure that text is currently selected in the text box.
195
if (this.myCMS_Object.SelectedText != "")
196
{
197
// Cut the selected text in the control and paste it into the Clipboard.
198
this.myCMS_Object.Cut();
199
}
200
}
201
202
/**//// <summary>
203
/// 复制
204
/// </summary>
205
/// <param name="sender"></param>
206
/// <param name="e"></param>
207
private void TSMI_Copy_Click(object sender, EventArgs e)
208
{
209
if (this.myCMS_Object.SelectionLength > 0)
210
{
211
this.myCMS_Object.Copy();
212
}
213
}
214
215
/**//// <summary>
216
/// 粘贴
217
/// </summary>
218
/// <param name="sender"></param>
219
/// <param name="e"></param>
220
private void TSMI_Paste_Click(object sender, EventArgs e)
221
{
222
// Determine if there is any text in the Clipboard to paste into the text box.
223
if (System.Windows.Forms.Clipboard.GetDataObject().GetDataPresent(System.Windows.Forms.DataFormats.Text) == true)
224
{
225
// Determine if any text is selected in the text box.
226
if (this.myCMS_Object.SelectionLength > 0)
227
{
228
// Ask user if they want to paste over currently selected text.
229
if (System.Windows.Forms.MessageBox.Show("Do you want to paste over current selection?", "Cut Example",
230
System.Windows.Forms.MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No)
231
// Move selection to the point after the current selection and paste.
232
this.myCMS_Object.SelectionStart = this.myCMS_Object.SelectionStart + this.myCMS_Object.SelectionLength;
233
}
234
// Paste current text in Clipboard into text box.
235
this.myCMS_Object.Paste();
236
}
237
}
238
239
/**//// <summary>
240
/// 删除
241
/// </summary>
242
/// <param name="sender"></param>
243
/// <param name="e"></param>
244
private void TSMI_Delete_Click(object sender, EventArgs e)
245
{
246
this.myCMS_Object.SelectedText = string.Empty;
247
}
248
249
/**//// <summary>
250
/// 全选
251
/// </summary>
252
/// <param name="sender"></param>
253
/// <param name="e"></param>
254
private void TSMI_SelectAll_Click(object sender, EventArgs e)
255
{
256
// sender as System.Windows.Forms.TextBoxBase;
257
// Determine if any text is selected in the TextBox control.
258
if (this.myCMS_Object.SelectionLength == 0)
259
// Select all text in the text box.
260
this.myCMS_Object.SelectAll();
261
262
// Copy the contents of the control to the Clipboard.
263
this.myCMS_Object.Copy();
264
265
}
266
267
/**//// <summary>
268
/// 在控件上按下鼠标事件处理方法
269
/// </summary>
270
/// <param name="sender"></param>
271
/// <param name="e"></param>
272
private void myCMS_Object_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
273
{
274
// this.myCMS_Object.ContextMenuStrip = this;
275
(sender as System.Windows.Forms.TextBoxBase).ContextMenuStrip = this;
276
}
277
278
/**//// <summary>
279
/// 单击控件事件处理方法
280
/// </summary>
281
/// <param name="sender"></param>
282
/// <param name="e"></param>
283
private void myCMS_Object_Click(object sender, EventArgs e)
284
{
285
if (this.Integer != 0)
286
{
287
this.myCMS_Object.SelectAll();
288
}
289
this.Integer = this.myCMS_Object.SelectionLength;
290
}
291
292
/**//// <summary>
293
/// 为控件设置输入焦点
294
/// </summary>
295
/// <param name="control">设置控件</param>
296
private void ControlSetFocus(System.Windows.Forms.Control control)
297
{
298
// 如果控件接受焦点,放置焦点到控件中。
299
if (control.CanFocus)
300
{
301
control.Focus();
302
}
303
}
304
305
当上下文菜单弹出事件处理方法#region 当上下文菜单弹出事件处理方法
306
307
/**//// <summary>
308
/// 当上下文菜单弹出事件处理方法
309
/// </summary>
310
/// <param name="sender"></param>
311
/// <param name="e"></param>
312
private void myCMS_Opened(object sender, EventArgs e)
313
{
314
// 让焦点进入控件this.myCMS_Object
315
this.ControlSetFocus(this.myCMS_Object);
316
317
this.myCMS_Object_OpenedMethod();
318
}
319
320
/**//// <summary>
321
///
322
/// </summary>
323
private void myCMS_Object_OpenedMethod()
324
{
325
this.InitItemEnableInMyCMS();
326
327
// 如果该控件中能否撤销前一操作
328
if (this.myCMS_Object.CanUndo == true)
329
{
330
this.TSMI_Undo.Enabled = true;
331
}
332
333
// 如果选中控件中的文本
334
if (this.myCMS_Object.SelectedText.Length > 0)
335
{
336
this.TSMI_Cut.Enabled = true;
337
338
this.TSMI_Copy.Enabled = true;
339
340
this.TSMI_Delete.Enabled = true;
341
}
342
343
// 如果剪切板中有文本字符
344
if (System.Windows.Forms.Clipboard.GetText().Length > 0)
345
{
346
this.TSMI_Paste.Enabled = true;
347
}
348
349
// 如果控件中不为空,则全选按钮使能为true
350
if (this.myCMS_Object.TextLength > 0)
351
{
352
this.TSMI_SelectAll.Enabled = true;
353
}
354
355
}
356
357
#endregion 当上下文菜单弹出事件处理方法
358
359
/**//// <summary>
360
/// 初始化选项按钮使能为false
361
/// </summary>
362
private void InitItemEnableInMyCMS()
363
{
364
try
365
{
366
foreach (System.Windows.Forms.ToolStripItem item in this.Items)
367
{
368
// 因为this.Items中有分隔条TSS(ToolStripSeparator)类,
369
// 与TSMI(ToolStripMenuItem)不同类,但由TSI(ToolStripItem)类直接和间接派生得来,
370
// 所以做如下判断,若是TSS类,无使能属性。
371
if(item is System.Windows.Forms.ToolStripMenuItem)
372
item.Enabled = false;
373
}
374
}
375
catch (InvalidCastException ICex)
376
{
377
}
378
}
379
}