VB.NET - Strange about clipboard and RichTextBox

manuel angeli 40 Reputation points
2023-09-13T12:33:35.0366667+00:00

Hi all.

I have a strange problem using the clipboard in a RichTecxBox control. I need to copy and paste rtf text with different formatting.

I can without problems copy the content (if I do from the keyboard ctrl+v paste fine) however I can't perfect the code to paste.

Clipboard.SetText(txt_note.Rtf, TextDataFormat.Rtf)

txt_note.Clear()

To paste I have tried several ways but none of them work.

Do you kindly have any advice?

Thank you in advance.

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,805 questions
{count} votes

Accepted answer
  1. Anonymous
    2023-09-15T09:44:02.49+00:00

    Hi

    OK, try this approach. To test, in an existing project which already has some highly formatted text in a RichTextBox1, I added a Button4 and this code. Works OK and may be what you are asking for. I put the text to be inserted into a variable to show the code more clearly.

    BTW: when you post code, just copy and paste into your message here and it will show up in a proper code box (like below).

    	Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
    
    		
    		Dim s As String = "Manuel graphics time " & " data 1 " & " at " & " data 2 " & " / h 0:05 - € 5,00" & vbCrLf & "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -" & vbCrLf
    
    		With RichTextBox1
    			.SelectAll()
    			.Copy()
    			.Text = s
    			.SelectionStart = .TextLength
    			.Paste()
    		End With
    	End Sub
    
    1 person found this answer helpful.

3 additional answers

Sort by: Most helpful
  1. manuel angeli 40 Reputation points
    2023-09-13T14:38:57.2866667+00:00

    Like in image, different background color and bold.

    dfg

    0 comments No comments

  2. Anonymous
    2023-09-13T14:42:13.55+00:00

    Hi

    I use without issue:

    
    		' copy selected text + format to clipboard
    		RichTextBox1.Copy()
    
    		' paste clipboard text with formatting
    		' to RichTextBox selected location
    		RichTextBox1.Paste()
    
    

  3. Jiachen Li-MSFT 34,201 Reputation points Microsoft External Staff
    2023-09-15T09:22:44.9366667+00:00

    Hi @manuel angeli ,

    Try using the following code to ensure that rich text is copied to the clipboard

    Clipboard.SetText(txt_note.Rtf, TextDataFormat.Rtf)
    

    Best Regards.

    Jiachen Li


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.