c# RTB - paste plain text without colours/fonts?

前端 未结 10 1184
感动是毒
感动是毒 2021-02-06 07:42

I am using Rich Text object in my C# application. The only issue I am having is that when user pastes formated text from another app, it remains formated which I cannot have. Is

相关标签:
10条回答
  • 2021-02-06 08:23

    Add a handler to the KeyDown-event to intercept the standard paste and manually insert only the plain text:

    private void rtb_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Control && e.KeyCode == Keys.V)
        {
            ((RichTextBox)sender).Paste(DataFormats.GetFormat("Text"));
                e.Handled = true;
        }
    }
    
    0 讨论(0)
  • 2021-02-06 08:27

    Assuming WinForms : try this : define a RichTextBox with a KeyDown event handler like this :

    Append-only example :

        private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control && e.KeyCode == Keys.V) 
            {
                richTextBox1.Text += (string)Clipboard.GetData("Text"); 
                e.Handled = true; 
            }
        }
    

    [Edit]

    Add Clipboard RTF to RichTextBox at current insertion point (selection start) example :

    private void richTextBox1_KeyDown(object sender, KeyEventArgs e) 
    { 
        if (e.Control && e.KeyCode == Keys.V)  
        { 
                // suspend layout to avoid blinking
                richTextBox2.SuspendLayout();
    
                // get insertion point
                int insPt = richTextBox2.SelectionStart;
    
                // preserve text from after insertion pont to end of RTF content
                string postRTFContent = richTextBox2.Text.Substring(insPt);
    
                // remove the content after the insertion point
                richTextBox2.Text = richTextBox2.Text.Substring(0, insPt);
    
                // add the clipboard content and then the preserved postRTF content
                richTextBox2.Text += (string)Clipboard.GetData("Text") + postRTFContent;
    
                // adjust the insertion point to just after the inserted text
                richTextBox2.SelectionStart = richTextBox2.TextLength - postRTFContent.Length;
    
                // restore layout
                richTextBox2.ResumeLayout();
    
                // cancel the paste
                e.Handled = true;
        } 
    } 
    

    [End Edit]

    Note 0 : The pasted in text is going to assume the current style settings in effect for the RichTextBox : if you have 'ForeGround color set to 'Blue : the pasted in text is going to be in blue.

    Note 1 : This is something I knocked together quickly, and tested only a few times by creating some multi-colored and weirdly formatted RTF for the clipboard using WordPad : then pasting into into the RichTextBox1 at run-time : it did strip away all the color, indenting, etc.

    Since it's not fully tested, use caution.

    Note 2 : This will not handle the case of 'Insert or 'Paste via Context Menu, obviously.

    Welcome all critiques of this answer, and will immediately take it down if it's not "on the mark."

    0 讨论(0)
  • 2021-02-06 08:27

    You can also use

    private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Control && e.KeyCode == Keys.V)
        {
            richTextBox1.SelectedText = (string)Clipboard.GetData("Text");
            e.Handled = true;
        }
    }
    
    0 讨论(0)
  • 2021-02-06 08:28

    I achieved this by udpating the font and colour for the whole RTB when its contents are changed. This works fine for me as the entry box does not need to deal with huge amounts of text.

    public FormMain()
    {
        InitializeComponent();
        txtRtb.TextChanged += txtRtb_TextChanged;
    }
    
    void txtRtb_TextChanged(object sender, EventArgs e)
    {
        RichTextBox rtb = (RichTextBox)sender;
        rtb.SelectAll();
        rtb.SelectionFont = rtb.Font;
        rtb.SelectionColor = System.Drawing.SystemColors.WindowText;
        rtb.Select(rtb.TextLength,0);
    }
    
    0 讨论(0)
提交回复
热议问题