Home > OS >  Copy selected text of a RichTextBox to the Clipboard with a Mouse Click
Copy selected text of a RichTextBox to the Clipboard with a Mouse Click

Time:02-20

I have a RichTextBox(RTB) that obtains text from two sources a txt file and data from a SQLite DB.
The RTB has ReadOnly property set to False.

I am trying to NOT use the ContextMenuStrip. This may be impossible ?
Here is the process fetch data from DB and populate the RTB
Highlight text I want to copy and click inside the RTB RESULT NO Copy

Second process tried same fetch data from DB this time I click on the form my code runs but no text is copied.
Here is the Code NOT neat but I am testing.

Public Class frmViewCode
Dim gvW As String

Public Sub rtbViewCode_MouseDown(sender As Object, e As MouseEventArgs) Handles MyBase.MouseDown
    If e.Button = System.Windows.Forms.MouseButtons.Left Then
        BigCopy()
        MsgBox("It Worked")
    End If
End Sub

OK I Changed BigCopy now the Selected code is added to the Clipboard
But I still need to click on the Form to execute the code.
Still would like to click on the RTB to fire the BigCopy code ?

    Public Sub BigCopy()
    Dim start = rtbViewCode.SelectionStart
    Dim substring = rtbViewCode.Text.Substring(0, start)
    Dim words = substring.Split(New String() {" ", vbCrLf}, StringSplitOptions.None)
    Dim count = words.Length
    gvW = rtbViewCode.SelectedText
    Clipboard.SetText(gvW)
End Sub

I am curious as how to change the System.Windows.Forms so I can click on the RTB.
The Question How to select text in a RTB and copy that text to the Clipboard with the Mouse?

I also tried this code:

    Public Sub RightMouse_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim start = rtbViewCode.SelectionStart
    Dim substring = rtbViewCode.Text.Substring(0, start)
    Dim words = substring.Split(New String() {" ", vbCrLf}, StringSplitOptions.None)
    Dim count = words.Length
    MsgBox("Len " & count)
    lblMsg.Text = count.ToString()
    'gvW = words.ToString
    'gvW = rtbViewCode.SelectedText
    'Clipboard.SetText(gvW)

    Clipboard.SetText(rtbViewCode.SelectedRtf, TextDataFormat.Rtf)
    MsgBox("here " & gvW)
End Sub

CodePudding user response:

I think you'd better off using a ContextMenuStrip. Anyway, you can use a Custom Control to trap WM_LBUTTONDOWN and perform a copy when the Left Mouse Button is pressed, there's an active selection and the mouse Pointer is inside the selection.
In this case, you suppress the message, so the selection is not removed.

Set HideSelection = False if you want to keep the selection visible while moving the focus to other Controls.

If this is not exactly the intended behavior, change the IsMouseDownInsideSelection() method or the logic in WM_LBUTTONDOWN.

Public Class RichTextBoxEx
    Inherits RichTextBox

    Private Const WM_LBUTTONDOWN As Integer = &H201

    Protected Overrides Sub WndProc(ByRef m As Message)
        If m.Msg = WM_LBUTTONDOWN Then
            If SelectionLength > 0 AndAlso IsMouseDownInsideSelection() Then
                Copy()
                Return
            End If
        End If
        MyBase.WndProc(m)
    End Sub

    Private Function IsMouseDownInsideSelection() As Boolean
        Dim charIdx = GetCharIndexFromPosition(PointToClient(MousePosition))
        If charIdx >= SelectionStart AndAlso charIdx <= (SelectionStart   SelectionLength) Then Return True
        Return False
    End Function
End Class

CodePudding user response:

You Can Use Clipboard.SetText(RichTextBox1.Text) Function To Copy All Text To Clipboard

  • Related