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
|
public static void ExtendWithContextMenu(this RichTextBox rtb)
{
ContextMenuStrip ctx = new ContextMenuStrip();
ToolStripMenuItem cut = new ToolStripMenuItem(
"Ausschneiden", null, (sender, e) => rtb.Cut());
ToolStripMenuItem copy = new ToolStripMenuItem(
"Kopieren", null, (sender, e) => rtb.Copy());
ToolStripMenuItem paste = new ToolStripMenuItem(
"Einfügen", null, (sender, e) => rtb.Paste());
ctx.Items.Add(cut);
ctx.Items.Add(copy);
ctx.Items.Add(paste);
rtb.ContextMenuStrip = ctx;
ctx.Opening +=
(sender, e) =>
{
bool noSelectedText = string.IsNullOrEmpty(rtb.SelectedText);
cut.Enabled = !noSelectedText;
copy.Enabled = !noSelectedText;
bool noClipboardText = string.IsNullOrEmpty(Clipboard.GetText());
paste.Enabled = !noClipboardText;
};
}
|