我有一個 RichTextBox(RTB),它從兩個源獲取文本,一個 txt 檔案和一個 SQLite DB 的資料。
RTB 的ReadOnly屬性設定為False。
我試圖不使用 ContextMenuStrip。這可能是不可能的?
這是從資料庫獲取資料并填充 RTB
突出顯示文本的程序,我要復制并在 RTB 結果中單擊 NO Copy
第二個程序嘗試從資料庫中獲取相同的資料,這次我單擊了我的代碼運行的表單,但沒有復制任何文本。
這是代碼不整潔,但我正在測驗。
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
但是我仍然需要單擊表單來執行代碼。
還想點擊 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
我很好奇如何更改 System.Windows.Forms 以便我可以點擊 RTB。
問題如何在 RTB 中選擇文本并使用滑鼠將該文本復制到剪貼板?
我也試過這段代碼:
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
uj5u.com熱心網友回復:
我認為您最好使用 ContextMenuStrip。無論如何,您可以使用自定義控制元件來捕獲WM_LBUTTONDOWN并在按下滑鼠左鍵時執行復制,有一個活動選擇并且滑鼠指標位于選擇內部。
在這種情況下,您會隱藏訊息,因此不會洗掉選擇。
設定HideSelection = False是否要在將焦點移動到其他控制元件時保持選擇可見。
如果這不完全是預期的行為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
uj5u.com熱心網友回復:
您可以使用Clipboard.SetText(RichTextBox1.Text)功能將所有文本復制到剪貼板
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/429707.html
上一篇:C#不必要地添加00:00:00
