简体   繁体   English

使用鼠标单击将 RichTextBox 的选定文本复制到剪贴板

[英]Copy selected text of a RichTextBox to the Clipboard with a Mouse Click

I have a RichTextBox(RTB) that obtains text from two sources a txt file and data from a SQLite DB.我有一个 RichTextBox(RTB),它从两个来源获取文本,一个 txt 文件和一个 SQLite 数据库的数据。
The RTB has ReadOnly property set to False . RTB 的ReadOnly属性设置为False

I am trying to NOT use the ContextMenuStrip.我试图不使用 ContextMenuStrip。 This may be impossible?这可能是不可能的?
Here is the process fetch data from DB and populate the RTB这是从数据库中获取数据并填充 RTB 的过程
Highlight text I want to copy and click inside the RTB RESULT NO Copy突出显示我要复制的文本并在 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好的,我更改了 BigCopy,现在所选代码已添加到剪贴板
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?还是想点击 RTB 来触发 BigCopy 代码?

    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.我很好奇如何更改 System.Windows.Forms 以便我可以单击 RTB。
The Question How to select text in a RTB and copy that text to the Clipboard with the Mouse?问题 如何在 RTB 中输入 select 文本并使用鼠标将该文本复制到剪贴板?

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

I think you'd better off using a ContextMenuStrip.我认为您最好使用 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.无论如何,您可以使用自定义控件捕获WM_LBUTTONDOWN并在按下鼠标左键时执行复制,有一个活动选择并且鼠标指针位于选择内。
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.如果要在将焦点移动到其他控件时保持选择可见,请设置HideSelection = False

If this is not exactly the intended behavior, change the IsMouseDownInsideSelection() method or the logic in WM_LBUTTONDOWN .如果这不是预期的行为,请更改IsMouseDownInsideSelection()方法或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

You Can Use Clipboard.SetText(RichTextBox1.Text) Function To Copy All Text To Clipboard您可以使用Clipboard.SetText(RichTextBox1.Text) Function 将所有文本复制到剪贴板

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM