Enable copy, cut, past window in a rich text box

前端 未结 7 1528
礼貌的吻别
礼貌的吻别 2021-01-31 09:30

I have a rich text box(richTextBox1) in my program as shown bellow. But when I right click on it, it doesn\'t pop up a “copy, cut, past” window. Can you please tell

7条回答
  •  一个人的身影
    2021-01-31 09:42

    Thanks to @Jaex

    https://stackoverflow.com/a/36624233/5132252

    https://stackoverflow.com/a/435510/5132252

        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Winapi)]
        internal static extern IntPtr GetFocus();
    
        private Control GetFocusedControl()
        {
            Control focusedControl = null;
            // To get hold of the focused control:
            IntPtr focusedHandle = GetFocus();
            if (focusedHandle != IntPtr.Zero)
                // Note that if the focused Control is not a .Net control, then this will return null.
                focusedControl = Control.FromHandle(focusedHandle);
            return focusedControl;
        }
    
        private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
        {
    
            if (Clipboard.ContainsText())
            {
                var FocusedControl = GetFocusedControl();
    
                if (FocusedControl != null)
                    switch (FocusedControl.GetType().Name)
                    {
                        case "RichTextBox":
                            {
                                var RichTextBox = FocusedControl as RichTextBox;
                                RichTextBox.Paste();
                                break;
                            }
                        case "TextBox":
                            {
                                var TextBox = FocusedControl as TextBox;
                                TextBox.Paste();
                                break;
                            }
    
                    }
            }
        }
    

提交回复
热议问题