Solved

C# RichTextBox contents to clipboard.

Posted on 2011-03-18
9
3,664 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

749 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