Solved

C# RichTextBox contents to clipboard.

Posted on 2011-03-18
9
3,615 Views
Last Modified: 2013-12-17
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

0
Comment
Question by:whorsfall
9 Comments
 
LVL 6

Expert Comment

by:openshac
ID: 35164699
          IDataObject data = Clipboard.GetDataObject();
   
          if (data.GetDataPresent(DataFormats.Rtf))
          {
               strPastedText = (string) data.GetData(DataFormats.Rtf);
               Invalidate();
          }

Open in new window

0
 

Author Comment

by:whorsfall
ID: 35165005
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
0
 
LVL 8

Expert Comment

by:mac-will
ID: 35165353
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

0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 8

Expert Comment

by:mac-will
ID: 35165365
btw I should give credit to this link:

http://www.codeproject.com/KB/shell/clipboard01.aspx
0
 
LVL 11

Expert Comment

by:SAMIR BHOGAYTA
ID: 35167884
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
0
 

Author Comment

by:whorsfall
ID: 35169956
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.
0
 
LVL 8

Accepted Solution

by:
mac-will earned 500 total points
ID: 35180339
I think I see what is happening.

\line in Rtf is being replace with \n only NOT \r\n as is expected on a windows system.

Try this and read the comment.

            DataObject m_data = new DataObject();

            m_data.SetData(DataFormats.Rtf, true, richTextBox1.Rtf);
            // NOTE: '\line' converts to a linefeed only.  Replace linefeed only with \r\n
            // However '\par' converts to \r\n so you may want to use a regex to make sure you
            // are only replacing \n that are not preceeded by \r
            m_data.SetData(DataFormats.Text, true, richTextBox1.Text.Replace("\n","\r\n"));
            Clipboard.SetDataObject(m_data, true);

Open in new window

0
 

Author Comment

by:whorsfall
ID: 35180644
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
0
 
LVL 8

Expert Comment

by:mac-will
ID: 35181761
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.)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question