Solved

C# RichTextBox contents to clipboard.

Posted on 2011-03-18
9
3,578 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
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…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

770 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