TsmiCut.Enabled = (SelectionLength != 0) & !this.ReadOnly TsmiRedo.Enabled = CanRedo & !this.ReadOnly TsmiUndo.Enabled = CanUndo & !this.ReadOnly TsmiSelectAll.ShortcutKeys = Keys.A | Keys.Control Ĭms.Opening += delegate (object sender, CancelEventArgs e) Public partial class RichTextBo圎x : RichTextBoxĬontextMenuStrip cms = new ContextMenuStrip It is also good to support other useful standard actions like Undo, Redo, Delete, and Select All. You can also define handler for context menu opening to check if any text is selected, clipboard is not empty and if the control is not set as read only. New component can be added from Solution Explorer project context menu Add -> New Item. In case that you need to add standard context menu to multiple RichTextBox instances then it may be better to create custom extended component inherited from RichTextBox. += Clipboard.GetText(TextDataFormat.Text).ToString() If you want to copy paste with another application like notepad (without styles ) please replace following methods void CopyAction(object sender, EventArgs e)Ĭlipboard.SetText(richTextBox1.SelectedText) = Clipboard.GetData(DataFormats.Rtf).ToString() If (Clipboard.ContainsText(TextDataFormat.Rtf)) Void PasteAction(object sender, EventArgs e) Void CopyAction(object sender, EventArgs e)Ĭlipboard.SetData(DataFormats.Rtf, richTextBox1.SelectedRtf) Void CutAction(object sender, EventArgs e) MenuItem.Click += new EventHandler(PasteAction) MenuItem.Click += new EventHandler(CopyAction) MenuItem.Click += new EventHandler(CutAction) If (e.Button = .Right)ĬontextMenu contextMenu = new () UPDATED CODE: private void richTextBox1_MouseUp(object sender, MouseEventArgs e) TsmiSelectAll.Enabled = rtb.TextLength > 0 & rtb.SelectionLength < rtb.TextLength Īnd use it like this: richTextBox1.AddContextMenu() TsmiDelete.Enabled = !rtb.ReadOnly & rtb.SelectionLength > 0 TsmiPaste.Enabled = !rtb.ReadOnly & Clipboard.ContainsText() TsmiCopy.Enabled = rtb.SelectionLength > 0 TsmiCut.Enabled = !rtb.ReadOnly & rtb.SelectionLength > 0 TsmiRedo.Enabled = !rtb.ReadOnly & rtb.CanRedo TsmiUndo.Enabled = !rtb.ReadOnly & rtb.CanUndo TsmiSelectAll.Click += (sender, e) => rtb.SelectAll() ToolStripMenuItem tsmiSelectAll = new ToolStripMenuItem("Select All") TsmiDelete.Click += (sender, e) => rtb.SelectedText = "" ToolStripMenuItem tsmiDelete = new ToolStripMenuItem("Delete") TsmiPaste.Click += (sender, e) => rtb.Paste() ToolStripMenuItem tsmiPaste = new ToolStripMenuItem("Paste") TsmiCopy.Click += (sender, e) => rtb.Copy() ToolStripMenuItem tsmiCopy = new ToolStripMenuItem("Copy") TsmiCut.Click += (sender, e) => rtb.Cut() ToolStripMenuItem tsmiCut = new ToolStripMenuItem("Cut") TsmiRedo.Click += (sender, e) => rtb.Redo() ToolStripMenuItem tsmiRedo = new ToolStripMenuItem("Redo") TsmiUndo.Click += (sender, e) => rtb.Undo() ToolStripMenuItem tsmiUndo = new ToolStripMenuItem("Undo") If you have more than one RichTextBox then you can use this extension method: public static void AddContextMenu(this RichTextBox rtb)ĬontextMenuStrip cms = new ContextMenuStrip()
0 Comments
Leave a Reply. |