سؤال

If I copy some text with different format and paste it to my richtextbox it is not plain I mean its format will be copied as well.

Is there anyway I can copy-paste as a plain text? By the way my program is on WinForm

thanks for any answer

هل كانت مفيدة؟

المحلول

you must use WinForm RichTextBox (not in UI, just in code), even if you are on WPF, in order to convert RTF to plain text. Use this method in your Copy event.

C# code :

private String ConvertRtfToText()
{
    System.Windows.Forms.RichTextBox rtfBox = new System.Windows.Forms.RichTextBox();
    rtfBox.Rtf = this.rtfData;

    return rtfBox.Text;
}

VB.Net Code :

Private Function ConvertRtfToText() As String
    Dim rtfBox As RichTextBox = New RichTextBox()
    rtfBox.Rtf = Me.rtfData
    Return rtfBox.Text
End Function

source : http://msdn.microsoft.com/en-US/en-en/library/vstudio/cc488002.aspx

نصائح أخرى

I recently had the same issue. I did want to retain some of the formatting, i.e. paragraphs and line feeds, but I required all the addition text format to be removed.

I'm working in WPF but the RichTextBox interface is the same. I have created a button that will allow users to select some text and remove the formatting. It is very simple, you just need to use the ClearAllProperties() method on the TextSelection object.

C# Code (WPF):

    private void ClearFormat_Click(object sender, RoutedEventArgs e)
    {           
        rtbText.Selection.ClearAllProperties();
    }

This is a super easy solution but perhaps not super elegant...

1) Add a plain textbox to your form and make it hidden

2) Create a button to remove the formatting (or you can do this automatically when the text is pasted)

3) In the OnClick (or OnPaste) code just copy the text from the rich textbox control to the plain textbox control then copy the text from the plain textbox back to the rich textbox control (see example below)

private void btnRemoveFormatting_Click(object sender, EventArgs e)
{
    txtPlainText.Text = txtRTF.Text;
    txtRTF.Text = "";  // Required - this makes sure all formatting is gone
    txtRTF.Text = txtPlainText.Text;
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top