how to copy text in a richtextbox to clipboard
-
as topic, how to select the text in richtextbox and copy them to clipboard to be ready to paste in other document?
Are you selecting the text in code or is a user doing it? If your doing it, use the Select method of the RichTextBox:
RichTextBox1.Select() ' selects everything in the RichTextBox
RichTextBox1.Select( start, length ) ' selects everthing starting at position 'start' for 'length' charactersAfter the selection is made all you have to do to get it on the Clipboard is this:
Clipboard.SetDataObject(RichTextBox1.SelectedRtf)
or if you want just the text (without the formatting):
Clipboard.SetDataObject(RichTextBox1.SelectedText)
RageInTheMachine9532
-
Are you selecting the text in code or is a user doing it? If your doing it, use the Select method of the RichTextBox:
RichTextBox1.Select() ' selects everything in the RichTextBox
RichTextBox1.Select( start, length ) ' selects everthing starting at position 'start' for 'length' charactersAfter the selection is made all you have to do to get it on the Clipboard is this:
Clipboard.SetDataObject(RichTextBox1.SelectedRtf)
or if you want just the text (without the formatting):
Clipboard.SetDataObject(RichTextBox1.SelectedText)
RageInTheMachine9532
thank you for replying me!! i tried out ur solution and it works!! however the output actually is not my expectation.. for example, the text in my richtext box is like following: hello! how are u? i am fine... after i copy these 3 lines of texts into clipboard using
Clipboard.SetDataObject(RichTextBox1.SelectedText)
then paste it into notepad, the output is following: hello!how are u?i am fine... the 3 lines text become one line and all the text were combined?! what i actually expect is the same format as in the multiline richtextbox... can u figure out another solution for me?? i really appreciate ur help... thank you very much!! -
thank you for replying me!! i tried out ur solution and it works!! however the output actually is not my expectation.. for example, the text in my richtext box is like following: hello! how are u? i am fine... after i copy these 3 lines of texts into clipboard using
Clipboard.SetDataObject(RichTextBox1.SelectedText)
then paste it into notepad, the output is following: hello!how are u?i am fine... the 3 lines text become one line and all the text were combined?! what i actually expect is the same format as in the multiline richtextbox... can u figure out another solution for me?? i really appreciate ur help... thank you very much!!Easy! Replace every copy of ControlChars.Lf with ControlChars.CrLf before you set the DataObject on the Clipboard:
Dim strText As String strText = RichTextBox1.SelectedText.Replace(ControlChars.Lf, ControlChars.CrLf) Clipboard.SetDataObject(strText, True)
BTW: The 'True' in the SetDataObject just means that the data is to be left on the clipboard if your app quits. RageInTheMachine9532
-
Easy! Replace every copy of ControlChars.Lf with ControlChars.CrLf before you set the DataObject on the Clipboard:
Dim strText As String strText = RichTextBox1.SelectedText.Replace(ControlChars.Lf, ControlChars.CrLf) Clipboard.SetDataObject(strText, True)
BTW: The 'True' in the SetDataObject just means that the data is to be left on the clipboard if your app quits. RageInTheMachine9532
-
as topic, how to select the text in richtextbox and copy them to clipboard to be ready to paste in other document?
ClipBoard.Clear ClipBoard.SetText rtfText1.SelRTF ,vbCFRTF