RichTextBox实现鼠标右键(剪切,复制,粘贴)功能

时间:2023-01-31 16:55:34

        private static void InitRichTextBoxContextMenu(RichTextBox textBox)

        {

            //创建剪切子菜单

            var cutMenuItem = new System.Windows.Forms.MenuItem("剪切");

            cutMenuItem.Click += (sender, eventArgs) => textBox.Cut();

            //创建复制子菜单

            var copyMenuItem = new System.Windows.Forms.MenuItem("复制");

            copyMenuItem.Click += (sender, eventArgs) => textBox.Copy();

            //创建粘贴子菜单

            var pasteMenuItem = new System.Windows.Forms.MenuItem("粘贴");

            pasteMenuItem.Click += (sender, eventArgs) => textBox.Paste();

            //创建右键菜单并将子菜单加入到右键菜单中

            var contextMenu = new ContextMenu();

            contextMenu.MenuItems.Add(cutMenuItem);

            contextMenu.MenuItems.Add(copyMenuItem);

            contextMenu.MenuItems.Add(pasteMenuItem);

            textBox.ContextMenu = contextMenu;

        }