Vb.net 如何通过鼠标悬停事件在RichTextBox中选择单词
我在一个文本框里有一篇长文章。我想在这篇长文章中复制一个单词,只需将鼠标移到该单词上,然后等待2秒钟。我不想做任何像突出显示单词这样的事情。我可以知道如何让VB做到这一点吗Vb.net 如何通过鼠标悬停事件在RichTextBox中选择单词,vb.net,winforms,text,richtextbox,Vb.net,Winforms,Text,Richtextbox,我在一个文本框里有一篇长文章。我想在这篇长文章中复制一个单词,只需将鼠标移到该单词上,然后等待2秒钟。我不想做任何像突出显示单词这样的事情。我可以知道如何让VB做到这一点吗 谢谢我在网上找到了以下代码。(归功于Siri1008) 声明一个公共变量curWord 然后,在RichTextBox的MouseMove事件中,将 curWord=GetWordUnderMouse(Me.RichTextBox1,e.X,e.Y) 在窗体上放置计时器,并将其间隔设置为2000。在计时器事件中,将 MsgB
谢谢我在网上找到了以下代码。(归功于Siri1008) 声明一个公共变量curWord 然后,在RichTextBox的MouseMove事件中,将 curWord=GetWordUnderMouse(Me.RichTextBox1,e.X,e.Y) 在窗体上放置计时器,并将其间隔设置为2000。在计时器事件中,将 MsgBox(curWord) Me.Timer1.Enabled=False 在RichTextBox MouseHover事件中,启用计时器
瞧,这个词被选中了,没有突出显示文本框。当然,如果您只是复制单词,则不需要msgbox,但您应该能够对其进行排序。winforms、wpf或vba。和Textbox或richtextbox?窗口窗体和richtextbox。谢谢你的提问,很抱歉我的问题中没有足够的信息。
Public Function GetWordUnderMouse(ByRef Rtf As System.Windows.Forms.RichTextBox, ByVal X As Integer, ByVal Y As Integer) As String
On Error Resume Next
Dim POINT As System.Drawing.Point = New System.Drawing.Point()
Dim Pos As Integer, i As Integer, lStart As Integer, lEnd As Integer
Dim lLen As Integer, sTxt As String, sChr As String
'
POINT.X = X
POINT.Y = Y
GetWordUnderMouse = vbNullString
'
With Rtf
lLen = Len(.Text)
sTxt = .Text
Pos = Rtf.GetCharIndexFromPosition(POINT)
If Pos > 0 Then
For i = Pos To 1 Step -1
sChr = Mid(sTxt, i, 1)
If sChr = " " Or sChr = Chr(13) Or i = 1 Then
'if the starting character is vbcrlf then
'we want to chop that off
If sChr = Chr(13) Then
lStart = (i + 2)
Else
lStart = i
End If
Exit For
End If
Next i
For i = Pos To lLen
If Mid(sTxt, i, 1) = " " Or Mid(sTxt, i, 1) = Chr(13) Or i = lLen Then
lEnd = i + 1
Exit For
End If
Next i
If lEnd >= lStart Then
GetWordUnderMouse = Trim(Mid(sTxt, lStart, lEnd - lStart))
End If
End If
End With
End Function