Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. how to copy text in a richtextbox to clipboard

how to copy text in a richtextbox to clipboard

Scheduled Pinned Locked Moved Visual Basic
tutorialquestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MJay
    wrote on last edited by
    #1

    as topic, how to select the text in richtextbox and copy them to clipboard to be ready to paste in other document?

    D Z 2 Replies Last reply
    0
    • M MJay

      as topic, how to select the text in richtextbox and copy them to clipboard to be ready to paste in other document?

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      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' characters

      After 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

      M 1 Reply Last reply
      0
      • D Dave Kreskowiak

        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' characters

        After 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

        M Offline
        M Offline
        MJay
        wrote on last edited by
        #3

        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!!

        D 1 Reply Last reply
        0
        • M MJay

          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!!

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          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

          M 1 Reply Last reply
          0
          • D Dave Kreskowiak

            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

            M Offline
            M Offline
            MJay
            wrote on last edited by
            #5

            thanks man!! it works!! by the way do u know how create a floating pop up menu when i right click in the richtextbox?? wat i actually wan to do is create a copy command in the popup menu when user selected the text and right click...

            1 Reply Last reply
            0
            • M MJay

              as topic, how to select the text in richtextbox and copy them to clipboard to be ready to paste in other document?

              Z Offline
              Z Offline
              Zipadie Doodah
              wrote on last edited by
              #6

              ClipBoard.Clear ClipBoard.SetText rtfText1.SelRTF ,vbCFRTF

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups