Help with copy and paste in rich text box

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 9947 Views - Last Post: 02 July 2011 - 07:25 AM Rate Topic: -----

#1 m_wylie85  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 96
  • View blog
  • Posts: 899
  • Joined: 15-October 10

Help with copy and paste in rich text box

Posted 30 June 2011 - 12:19 PM

Hi all i am write a complaints form for work i am try the copy and paste with text boxes and rich text boxes i can get the text boxes to copy and paste but having a problem with figuring out how to do it with rich text boxes. I have a menu strip which controls the copy function (shown below)

    Private Sub CopyToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CopyToolStripMenuItem.Click
        'Copy Text from text box and place it in clipboard
        Clipboard.SetDataObject(CType(ActiveControl, TextBox).SelectedText)
    End Sub


But i want to be able to copy from a rich text box as well, that where i am having the trouble i know this line below will get text from a rich text box:
Clipboard.SetDataObject(CType(ActiveControl, RichTextBox).SelectedRtf)


but how to i check the form to see if it is trying to copy from a text box or a rich text box i mean something like this ( this not working code or in anyway right its just to show you what i mean.

    Private Sub CopyToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CopyToolStripMenuItem.Click
        'Copy Text from text box and place it in clipboard
        If activeControl is a textbox do below
        Clipboard.SetDataObject(CType(ActiveControl, TextBox).SelectedText)
        else if activeControl is a Richtextbox do below
        Clipboard.SetDataObject(CType(ActiveControl, RichTextBox).SelectedRtf)
    End Sub


i want to be able to check what type of text/rich text box is be use so that i can copy from a text box or a rich text box any help would be great

Is This A Good Question/Topic? 0
  • +

Replies To: Help with copy and paste in rich text box

#2 Zizeeo  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 106
  • Joined: 06-April 11

Re: Help with copy and paste in rich text box

Posted 30 June 2011 - 12:40 PM

I'm not clear on what you're trying to do. Are you trying to copy text from a Rich text box and set it to you clipboard?

This post has been edited by Zizeeo: 30 June 2011 - 12:42 PM

Was This Post Helpful? 0
  • +
  • -

#3 m_wylie85  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 96
  • View blog
  • Posts: 899
  • Joined: 15-October 10

Re: Help with copy and paste in rich text box

Posted 30 June 2011 - 12:45 PM

yes but i also want to be able to copy text from a text box aswell
Was This Post Helpful? 0
  • +
  • -

#4 Zizeeo  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 106
  • Joined: 06-April 11

Re: Help with copy and paste in rich text box

Posted 30 June 2011 - 12:56 PM

OK, try typing
Clipboard.SetText(Richtextbox1.Text)
Then for the Text box you only need to switch out the control. Is that all you needed?
Was This Post Helpful? 0
  • +
  • -

#5 m_wylie85  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 96
  • View blog
  • Posts: 899
  • Joined: 15-October 10

Re: Help with copy and paste in rich text box

Posted 30 June 2011 - 01:04 PM

Yeah i had try that but that means i would have to write that for every control i want to check what control is active and just copy from the active control no matter if it is a rich text box or text box can that be done
Was This Post Helpful? 0
  • +
  • -

#6 Zizeeo  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 106
  • Joined: 06-April 11

Re: Help with copy and paste in rich text box

Posted 30 June 2011 - 01:18 PM

Honestly, I don't know how to do that. Sorry, I wish I could help.
Was This Post Helpful? 0
  • +
  • -

#7 m_wylie85  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 96
  • View blog
  • Posts: 899
  • Joined: 15-October 10

Re: Help with copy and paste in rich text box

Posted 30 June 2011 - 01:20 PM

No problem cheer anyways
Was This Post Helpful? 0
  • +
  • -

#8 Zizeeo  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 106
  • Joined: 06-April 11

Re: Help with copy and paste in rich text box

Posted 30 June 2011 - 01:28 PM

Ya, I'll keep looking for a way but I'm not sure I will be able to figure it out.
Was This Post Helpful? 0
  • +
  • -

#9 m_wylie85  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 96
  • View blog
  • Posts: 899
  • Joined: 15-October 10

Re: Help with copy and paste in rich text box

Posted 30 June 2011 - 01:31 PM

Hey i think i have any idea but this is still rough way yet but looks good so far

    Private Sub CopyToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CopyToolStripMenuItem.Click
        'Copy Text from text box and place it in clipboard

  
        ' Determine the active child form.
        Dim activeChild As Form = Me.ActiveMdiChild

        ' If there is an active child form, find the active control, which
        ' in this example should be a RichTextBox.
        If (Not activeChild Is Nothing) Then
            Try
                Dim theRichTextBox As RichTextBox = _
                CType(activeChild.ActiveControl, RichTextBox)
                Dim theTextBox As TextBox = _
                    CType(activeChild.ActiveControl, TextBox)
                If (Not theRichTextBox Is Nothing) Then
                    ' Put selected text on Clipboard.
                    Clipboard.SetDataObject(theRichTextBox.SelectedText)
                ElseIf (Not theTextBox Is Nothing) Then
                    Clipboard.SetDataObject(theTextBox.SelectedText)
                End If
            Catch
                MessageBox.Show("You need to select a control.")
            End Try
        End If
    End Sub


got to love msdn

Just had to edit it a bit here is the link if you want to see

http://msdn.microsof...7(v=vs.71).aspx

This post has been edited by m_wylie85: 30 June 2011 - 01:32 PM

Was This Post Helpful? 0
  • +
  • -

#10 Zizeeo  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 106
  • Joined: 06-April 11

Re: Help with copy and paste in rich text box

Posted 30 June 2011 - 01:43 PM

There you go. I'm glad you can figure it out because I have no idea. I never did a lot of Visual Basic anyway I'm doing Java.
Was This Post Helpful? 0
  • +
  • -

#11 m_wylie85  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 96
  • View blog
  • Posts: 899
  • Joined: 15-October 10

Re: Help with copy and paste in rich text box

Posted 30 June 2011 - 01:49 PM

cheers mate if i need more help i will be back cheers
Was This Post Helpful? 0
  • +
  • -

#12 m_wylie85  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 96
  • View blog
  • Posts: 899
  • Joined: 15-October 10

Re: Help with copy and paste in rich text box

Posted 30 June 2011 - 02:31 PM

hey got it working here is the code encase you where wondering:

    Private Sub CopyToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CopyToolStripMenuItem.Click
        If TypeOf Me.ActiveControl Is TextBox Then
            Clipboard.SetDataObject(CType(ActiveControl, TextBox).SelectedText)
        ElseIf TypeOf Me.ActiveControl Is RichTextBox Then
            Clipboard.SetDataObject(CType(ActiveControl, RichTextBox).SelectedText)
        End If
    End Sub

    Private Sub PasteToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PasteToolStripMenuItem.Click
        'Retrieve data from clipboard and place it in text box
        Dim oDataObject As IDataObject
        oDataObject = Clipboard.GetDataObject()
        If TypeOf Me.ActiveControl Is TextBox Then
            CType(ActiveControl, TextBox).SelectedText = CType(oDataObject.GetData(DataFormats.Text), String)
        ElseIf TypeOf Me.ActiveControl Is RichTextBox Then
            CType(ActiveControl, RichTextBox).SelectedText = CType(oDataObject.GetData(DataFormats.Text), String)
        End If
    End Sub

Was This Post Helpful? 0
  • +
  • -

#13 Zizeeo  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 106
  • Joined: 06-April 11

Re: Help with copy and paste in rich text box

Posted 30 June 2011 - 02:59 PM

That's awesome but way above my Visual Basic level.
Was This Post Helpful? 0
  • +
  • -

#14 dbasnett  Icon User is offline

  • D.I.C Addict

Reputation: 108
  • View blog
  • Posts: 601
  • Joined: 01-October 08

Re: Help with copy and paste in rich text box

Posted 01 July 2011 - 04:36 AM

Maybe I a missing something. You can copy and paste the contents of a richtextbox and textbox without any code. I created a new project with a richtextbox and textbox, typed some stuff into one, selected / copied it, and then pasted it into the other.

This post has been edited by dbasnett: 01 July 2011 - 04:36 AM

Was This Post Helpful? 0
  • +
  • -

#15 m_wylie85  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 96
  • View blog
  • Posts: 899
  • Joined: 15-October 10

Re: Help with copy and paste in rich text box

Posted 01 July 2011 - 10:55 AM

yeah you can copy and paste using the mouse but to be able to do it with the key board
i need to do above ( u know CTrl C and CTrl Z and so on)
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2