-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExtensionMethod.cs
81 lines (67 loc) · 3.49 KB
/
ExtensionMethod.cs
1
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsTorWrapper
{
// Important ! Create the ExtensionMethods class as a "public static" class
public static class ExtensionMethods
{
public static void EnableContextMenu(this RichTextBox rtb)
{
if (rtb.ContextMenuStrip == null)
{
// Create a ContextMenuStrip without icons
ContextMenuStrip cms = new ContextMenuStrip();
cms.ShowImageMargin = false;
// 1. Add the Undo option
ToolStripMenuItem tsmiUndo = new ToolStripMenuItem("Undo");
tsmiUndo.Click += (sender, e) => rtb.Undo();
cms.Items.Add(tsmiUndo);
// 2. Add the Redo option
ToolStripMenuItem tsmiRedo = new ToolStripMenuItem("Redo");
tsmiRedo.Click += (sender, e) => rtb.Redo();
cms.Items.Add(tsmiRedo);
// Add a Separator
cms.Items.Add(new ToolStripSeparator());
// 3. Add the Cut option (cuts the selected text inside the richtextbox)
ToolStripMenuItem tsmiCut = new ToolStripMenuItem("Cut");
tsmiCut.Click += (sender, e) => rtb.Cut();
cms.Items.Add(tsmiCut);
// 4. Add the Copy option (copies the selected text inside the richtextbox)
ToolStripMenuItem tsmiCopy = new ToolStripMenuItem("Copy");
tsmiCopy.Click += (sender, e) => rtb.Copy();
cms.Items.Add(tsmiCopy);
// 5. Add the Paste option (adds the text from the clipboard into the richtextbox)
ToolStripMenuItem tsmiPaste = new ToolStripMenuItem("Paste");
tsmiPaste.Click += (sender, e) => rtb.Paste();
cms.Items.Add(tsmiPaste);
// 6. Add the Delete Option (remove the selected text in the richtextbox)
ToolStripMenuItem tsmiDelete = new ToolStripMenuItem("Delete");
tsmiDelete.Click += (sender, e) => rtb.SelectedText = "";
cms.Items.Add(tsmiDelete);
// Add a Separator
cms.Items.Add(new ToolStripSeparator());
// 7. Add the Select All Option (selects all the text inside the richtextbox)
ToolStripMenuItem tsmiSelectAll = new ToolStripMenuItem("Select All");
tsmiSelectAll.Click += (sender, e) => rtb.SelectAll();
cms.Items.Add(tsmiSelectAll);
// When opening the menu, check if the condition is fulfilled
// in order to enable the action
cms.Opening += (sender, e) =>
{
tsmiUndo.Enabled = !rtb.ReadOnly && rtb.CanUndo;
tsmiRedo.Enabled = !rtb.ReadOnly && rtb.CanRedo;
tsmiCut.Enabled = !rtb.ReadOnly && rtb.SelectionLength > 0;
tsmiCopy.Enabled = rtb.SelectionLength > 0;
tsmiPaste.Enabled = !rtb.ReadOnly && Clipboard.ContainsText();
tsmiDelete.Enabled = !rtb.ReadOnly && rtb.SelectionLength > 0;
tsmiSelectAll.Enabled = rtb.TextLength > 0 && rtb.SelectionLength < rtb.TextLength;
};
rtb.ContextMenuStrip = cms;
}
}
}
}