Solved

C# RichTextBox contents to clipboard.

Posted on 2011-03-18
9
3,477 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
Comment Utility
          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
Comment Utility
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
Comment Utility
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
 
LVL 8

Expert Comment

by:mac-will
Comment Utility
btw I should give credit to this link:

http://www.codeproject.com/KB/shell/clipboard01.aspx
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 11

Expert Comment

by:SAMIR BHOGAYTA
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

728 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now