Share via

RichTextBox - Problem "Paste" method not working if text copy from OneNote

Dirk Vatterott 21 Reputation points
2022-11-18T12:47:45.307+00:00

For certain reasons I need to handle the text when it is copied from the clipboard to the RichTextBox via ShortCut ctrl+v.

I use for this:
DataFormats.Format myFormat = DataFormats.GetFormat(DataFormats.Text);
if (textBox.CanPaste(myFormat))
{
textBox.Paste();
string pasteText = textBox.Text;

The problem is, if the text is copied from OneNode textBox.Text contains only a " " space.

If richtextbox handle this byself is it working.

If I use this code is also working:

        DataFormats.Format myFormat = DataFormats.GetFormat(DataFormats.Text);  
        if (textBox.CanPaste(myFormat))  
        {  
            string insertText = Clipboard.GetText();  
            if (!string.IsNullOrEmpty(insertText))  
            {  
                textBox.Text = insertText;  

Is the a bug or what do I wrong with "Paste" method?

I have a small test program where I can reporduce the problem. If need.

Thanks in advance.

Bye
Dirk

Developer technologies | Windows Forms

Answer accepted by question author

  1. Jack J Jun 25,306 Reputation points
    2022-11-21T10:28:52.33+00:00

    @Dirk Vatterott , thanks for the feedback, based on my test, I reproduced your problem.

    We need to use DataFormats.UnicodeText when we paste the format from OneNote to richtextbox.

    If you want to paste the format from the OneNote to Richtextbox and show the paste text, you could try the following code:

     internal void DoPasteAction(RichTextBox textBox)  
            {  
                DataFormats.Format myFormat = DataFormats.GetFormat(DataFormats.UnicodeText);  
                if (richTextBox1.CanPaste(myFormat))  
                {  
                    richTextBox1.Paste();  
                    string insertText = Clipboard.GetText();  
                    MessageBox.Show(insertText);  
                }  
            }  
    

    Result:

    262606-image.png

    Best Regards,
    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Was this answer helpful?

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Dirk Vatterott 21 Reputation points
    2022-11-21T06:52:15.65+00:00

    Here is the code:

    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    //this.richTextBox1.ShortcutsEnabled = false;
    }

        private void richTextBox1_KeyDown(object sender, KeyEventArgs e)  
        {  
            switch (e.KeyCode)  
            {  
                case Keys.V:  
                    if (e.Control)  
                    {  
                        this.DoPasteAction(this.richTextBox1);  
                        e.Handled = true;  
                    }  
                    break;  
                case Keys.Insert:  
                    if (e.Shift)  
                    {  
                        this.DoPasteAction(this.richTextBox1);  
                        e.Handled = true;  
                    }  
                    break;  
            }  
        }  
    
        /// <summary>  
        /// Does the paste action. Paste only Text  
        /// </summary>  
        internal void DoPasteAction(RichTextBox textBox)  
        {  
            DataFormats.Format myFormat = DataFormats.GetFormat(DataFormats.Text);  
            if (textBox.CanPaste(myFormat))  
            {  
                //Not working  
                textBox.Paste();  
                string pasteText = textBox.Text;  
                textBox.Text = CleanInputForXML(pasteText);  
    
                //Working  
                //string insertText = Clipboard.GetText();  
                //if (!string.IsNullOrEmpty(insertText))  
                //{  
                //    insertText = CleanInputForXML(insertText);  
                //    textBox.Text = insertText;  
                //}  
            }  
        }  
    
        public static string CleanInputForXML(string strIn)  
        {  
            // Replace invalid characters with empty strings.  
            try  
            {  
                return Regex.Replace(strIn, @"[^\w]", "", RegexOptions.None);  
            }  
            catch  
            {  
                return strIn;  
            }  
        }  
    
    
    }
    

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.