Link to home
Start Free TrialLog in
Avatar of whorsfall
whorsfallFlag for Australia

asked on

C# RichTextBox contents to clipboard.

Hi,

I have a RichTextBox with some colored (RTF) formatted text in it.
What I would like to do is be able to copy the contents to the clipboard, but do it like wordpad
does.

So for example if I type some and color it in there and cut and paste it maintains the formatting if
i pate it to another instance it maintains the formatting. If I paste it into notepad it drops the
formatting and pasts it as plain text becuase notepad does not support it.


                Clipboard.SetText(log_control.Text);
                Clipboard.SetText(log_control.Rtf, TextDataFormat.Rtf);

So my question is how to set the clipboard with C# so if the target app supports RTF it gets the
formatted text but if it is going to notepad it will downgrade. At the moment it seems like an either / or situation.

Thanks,

Ward

Avatar of openshac
openshac

          IDataObject data = Clipboard.GetDataObject();
   
          if (data.GetDataPresent(DataFormats.Rtf))
          {
               strPastedText = (string) data.GetData(DataFormats.Rtf);
               Invalidate();
          }

Open in new window

Avatar of whorsfall

ASKER

Hi,

Thanks for your response. I am not quite sure I get the example. From how I am reading it this
will read what is in the clipboard, where as my issue is how to paste something to the clipboard.

Thanks again,

Ward
Try this:

DataObject m_data = new DataObject();
            m_data.SetData(DataFormats.Rtf, true, richTextBox1.Rtf);
            m_data.SetData(DataFormats.Text, true, richTextBox1.Text);
            Clipboard.SetDataObject(m_data, true);

Open in new window

btw I should give credit to this link:

http://www.codeproject.com/KB/shell/clipboard01.aspx
Avatar of SAMIR BHOGAYTA
Hello, I have the solution but it is in vb.net if you want in C# then convert it into C#.

Public Sub CopyFromRichTextBox(ByVal rtb As RichTextBox, _
    Optional ByVal availableAfterEnd As Boolean = False)
    Dim data As New DataObject()

    ' get the selected RTF text if there is a selection,
    ' or the entire text is no text is selected
    Dim rtfText, plainText As String
    If rtb.SelectionLength > 0 Then
        rtfText = rtb.SelectedRtf
        plainText = rtb.SelectedText
    Else
        rtfText = rtb.Rtf
        plainText = rtb.Text
    End If
    ' do the copy only if there is something to be copied
    If rtfText.Length > 0 Then data.SetData(DataFormats.Rtf, rtfText)
    If plainText.Length > 0 Then data.SetData(DataFormats.Text, plainText)

    ' finally copy into the clipboard
    Clipboard.SetDataObject(data, availableAfterEnd)
End Sub
Hi,

Thanks for the help it almost works. The issue is the plain text that comes through is missing
all the newline characters. So the line breaks come through the RTF but not in the plain text.

Any ideas?

Thanks,

Ward.
ASKER CERTIFIED SOLUTION
Avatar of mac-will
mac-will
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi,

Thanks for your response, can u restate it a different way on what I should. Btw I will need to check this but it has \par intead on \line.

Thanks

Ward
I see, I just did a test and \par also converted to \n.
If this is the case just use the example code I provided above.

(My concern was if \par was replace with \r\n and you did the replace you would end up with \r\r\n, not the end of the world but not what you want either.)