如何在WinForms C#中的富文本框中实现复制,剪切和粘贴上下文菜单

本文概述

  • 1.创建ExtensionMethods类
  • 2.在RichTextBox上启用上下文菜单
默认情况下, Rich Text Boxes组件不提供任何实用工具来与文本编辑器进行最基本的用户交互, 这意味着复制, 剪切, 全选或删除一段文本。这意味着你将需要自己实现此功能。在本文中, 我们将向你解释如何在富文本框中添加上下文菜单栏, 以便用户能够使用常规的文本编辑器上下文菜单。
1.创建ExtensionMethods类【如何在WinForms C#中的富文本框中实现复制,剪切和粘贴上下文菜单】只是在MSDN网站上指定的, 当需要在项目中的多个地方实现某些功能时, 最后要做的就是在整个项目中重写实用程序。相反, 根据需要, RichtTextBox类型已经存在, 因此我们可以编写扩展而无需创建新的派生类型。
下面的示例演示如何创建一个扩展方法类, 该类将包含应该从任何RichTextBox实例均可调用的帮助器EnableContextMenu方法。创建ExtensionMethods.cs文件并注册以下类:
using System.Windows.Forms; // Important ! Create the ExtensionMethods class as a "public static" classpublic static class ExtensionMethods{public static void EnableContextMenu(this RichTextBox rtb){if (rtb.ContextMenuStrip == null){// Create a ContextMenuStrip without iconsContextMenuStrip cms = new ContextMenuStrip(); cms.ShowImageMargin = false; // 1. Add the Undo optionToolStripMenuItem tsmiUndo = new ToolStripMenuItem("Undo"); tsmiUndo.Click += (sender, e) => rtb.Undo(); cms.Items.Add(tsmiUndo); // 2. Add the Redo optionToolStripMenuItem tsmiRedo = new ToolStripMenuItem("Redo"); tsmiRedo.Click += (sender, e) => rtb.Redo(); cms.Items.Add(tsmiRedo); // Add a Separatorcms.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 Separatorcms.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 actioncms.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; }}}

所提到的方法会在RichTextBox内部的单击位置(右键单击)上动态创建ContextMenuStrip, 并根据其可用性提供一些选项。
2.在RichTextBox上启用上下文菜单如前所述, 你可能有多个富文本框, 它们需要相同的功能。先前创建的扩展允许专门从富文本框实例调用新方法:
richTextBox1.EnableContextMenu();

在某些情况下, 由于上下文(在你创建类的位置), 你可能需要将扩展??引入指令的作用域:
using ExtensionMethods;

因此, 例如在初始化非常简单的表单期间(在构造函数内部):
using System; using System.Windows.Forms; namespace Sandbox{public partial class Form1 : Form{public Form1(){InitializeComponent(); // Enable Context Menu !richTextBox1.EnableContextMenu(); }private void Form1_Load(object sender, EventArgs e){}}}

就像文章的屏幕截图所示, 每次你右键单击富文本框时, 都会出现菜单, 并且每个选项都将启用(如果可用)。
编码愉快!

    推荐阅读