Is it possible to emulate the 'Control+C' process?

DevilAkuma

Active member
Joined
Oct 24, 2005
Messages
37
Programming Experience
Beginner
Hello!

I have a richTextBox with text. I would have the typical context menu (with Copy, Paste, Undo, Cut...) but when I right-click in mi rich, no one appears!
So, I want to add some buttons to my taskbar (Sorry, I did'nt mention... I have a task bar too ;)) with the typical options: Copy, Paste, Find & Cut. Is it possible that when the user select some text and click my 'Copybutton' my application do the same as if the user press 'Ctrl + C'?

Thanks in advance!
 
This is the easiest solution I have found so far, it will work with all controls by delegating the Copy, Cut, Paste, ... implementation to windows :

VB.NET:
    Private Sub UndoToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UndoToolStripMenuItem.Click
        SendKeys.Send("^Z")
    End Sub

    Private Sub CutToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles CutToolStripMenuItem.Click
        SendKeys.Send("^X")
    End Sub

    Private Sub CopyToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles CopyToolStripMenuItem.Click
        SendKeys.Send("^C")
     End Sub

    Private Sub PasteToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles PasteToolStripMenuItem.Click
        SendKeys.Send("^V")
    End Sub

    Private Sub SelectAllToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        SendKeys.Send("^A")
    End Sub
 
Last edited:
I tried with the jmcilhinney's solution before posting here. Now I've tried with Bob Langlade's solution and more or less works But I can't fix my first problem... The action is done... when I selected a text, and click in my tool bar (not task bar, thanks) the text is pasted but is pasted with the color and the style typed in the rich text box. If I copy text that are written in red... When I paste this text, is red too.. Is it possible to desactivate this option. I want to copy only the text (without styles and colours)

Any idea?

Thanks in advance.
 
Before sending Ctrl C with SendKeys, you could try to modify the clipboard content to have the color and style you want.

I have never tried that, but you have many methods to work on the clipboard : ContainsText, GetText, SetText, Clear.
 
I have tried this and it works :

VB.NET:
    Private Sub PasteToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles PasteToolStripMenuItem.Click
        With My.Computer.Clipboard
            If .ContainsText Then
                Dim Toggle As String = .GetText
                .SetText(Toggle)
            End If
        End With
        SendKeys.Send("^V")
    End Sub
There must certainly be a cleaner way to do it (by removing formatting).
 
This one will work with VB 2003 :

VB.NET:
    Private Sub MnPaste_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnPaste.Click
        Dim ClipBoardData As IDataObject = Clipboard.GetDataObject
        'Test if data is in text format
        If ClipBoardData.GetDataPresent(DataFormats.Text) Then
            Dim Toggle As String = CType(ClipBoardData.GetData(DataFormats.Text), String)
            Clipboard.SetDataObject(Toggle)
        End If
        SendKeys.Send("^V")
    End Sub
 
Wow!

Bob Langlade you are my hero! Runs perfectly!

One question: Do you know why when y use the 'SendKeys.Send("^C")' method the selected text loses its style? For example, If I click my paste button (or click Control + C directly) in a red text, this text turns automatically black. So, I use the function richtextbox.copy(), but I don't know how force this function if the user press Control + C. Is any way to do this? Anybody knows why my colored text turns to black?

Thanks in advance
 
Last edited:
Sorry, but I'm spanish and my English is not so good :eek:

I want to lose formatting wher I paste text, but I want to keep formatting when I copy. Imagine that my richtextBox is an editor like .NET (whith some words in colour, and some words underlined because are wrong) I would like that if you copy a wrong word (in my rich, red word) when you paste it, the word changes to black (because is new!) I've reach that thanks to you.
But if you copy a wrong word, I wouldn't like lose his color, because the word still bad and I need that keep its color :confused:

Is more clear now? My english is not so good. Sorry and thanks
 
I have not found a way to check whether the text is underlined or not in the ClipBoard content before pasting the text :eek: (but I am no Expert).

In that case jmcilhinney's solution to use the RichTextBox class seems to be easier as you can verify with the SelectionFont property if your text is Underlined.

The problem you will have in either case is that your selection can contain different font styles.
 
Back
Top