Question The Joy of the Clipboard

fthomas137

Member
Joined
Oct 7, 2010
Messages
7
Programming Experience
Beginner
Hey all,

I'd like to ask if anyone knows how to properly use the clipboard to take information from a rich text box and send it to the clipboard. You see, if I try the following, which is pulling text from a rich text box, named RichTextBox2:

VB.NET:
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Clipboard.Clear()
        Clipboard.SetText(RichTextBox2.Text, TextDataFormat.Rtf)
        MsgBox("Spinnable Text Copied To Clipboard")
    End Sub

Now when I do this, I get nothing back. The clipboard is empty.

If I do this,

VB.NET:
Clipboard.SetText(RichTextBox2.Text, TextDataFormat.Text)

I will get the text from the text box, but all formatting, like carriage returns is lost. But if I just do a manual copy/paste from the box, all is well.

Any ideas the right way to do this? BTW, I'm using Visual Basic 2010.

Thanks way ahead of time!

Frank
 
Remove the Clipboard.Clear() call, when using any of the Clipboard.Set method the entire clipboard object is replaced.

The rtf content in RichTextBox you can get from Rtf property (or SelectedRtf), the Text property (or SelectedText) only return the plain text version of the content.

You can learn more about the RichTextBox online here: RichTextBox Class (System.Windows.Forms)
 
Thanks very much John,

But I"m still struggling.

I modified the said code to:

VB.NET:
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Clipboard.SetText(RichTextBox2.SelectedRtf, TextDataFormat.Rtf)
        MsgBox("Text Copied To Clipboard")
    End Sub

And now, I get nothing.

I've been tinkering for hours with this problem, but just can't seem to crack it. Also have googled my brains out. So, I do appologies for the questions as a newbie, I'm just really stuck.
 
Last edited:
And now, I get nothing.
Surely there must be something? :cool: I think I know what you mean. Likely you are trying to paste into a plain text box, for example Notepad, and since the only data format you have put in clipboard is rtf you get nothing, there is no plain text in clipboard. If you on the other hand paste into a rich text box, like Wordpad, you will get it. Get it?

You can learn more about the Clipboard class online here: Clipboard Class (System.Windows.Forms)
 
John,

Thanks again for your super fast response. I will try your suggestion in the morning. But I'd like to ask another question. The text I'm working with doesn't have to be rich text. It just has to keep it's formatting, like new lines and new paragraphs. In truth, more is less. I'm creating a series of applications that will allow me to automate the changing of text from one setup to another. In some cases, like this app, it literally changes {} to []. So, pasting in anything else, like bold, italics or special stuff is just too much. But I wanted to create two text boxes, before and after, that will show the text. Now you'll see how green I am. I figured the only opinion was the richtextboxes, as I couldn't find any other boxes that could be expanded to multiline. If you have a better recommendation that will better suit what I'm up to, I'm truly all ears!

And be gentle, I literally learn vb a week ago! But I'm a rapid learner with programming experience in non-object oriented languages. To be honest, I'm just cutting my teeth on language differences. For example, I just couldn't get a IF cond1 AND cond2 THEN to work properly. I eventually resorted to to IF statements, one after another.

Anyhow, I'd be extremely grateful for your insights!

Frank
 
With regular TextBox set Multiline property to True. If you're going to be handling plain text only just use the Clipboard.SetText(String) method. If you were using RichTextBox for some reason and still only wanted the plain text you'd get it from Text/SelectedText properties as mentioned.
 
Thanks again John! I'm going to look into this morning. And get it done. I will definitely post if I have further questions.

Thanks again!

Frank
 
John, all I have to say is 'YOU ROCK'!:D

No, don't let anyone tell you different!!

Thank you very, very much. I think I finally get it! I'm just amazed how much a user is shielded from the complexity of the interface.

Frank
 
Back
Top