0

我想在用户单击 contextmenustrip dorpdownitem 时替换鼠标光标下的当前单词。我可以得到这个词,但无法用新词替换这个词,这是我的代码,非常感谢任何帮助。

Private Sub tsmmutradifat__DropDownItemClicked(ByVal sender As Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles tsmmutradifat_.DropDownItemClicked
    Dim myclickeditem As String = correct_word(e.ClickedItem.Text)
    Dim wordtoreplace As String
    If Not myclickeditem = Nothing Then
        If RichTextBox1.SelectedText = "" Then
            ' Code required
        Else
            wordtoreplace = RichTextBox1.SelectedText
            If wordtoreplace.EndsWith(" ") Then
                myclickeditem = myclickeditem & " "
            End If
            If wordtoreplace.StartsWith(" ") Then
                myclickeditem = " " & myclickeditem
            End If
            RichTextBox1.SelectedText = myclickeditem
        End If
    End If
End Sub

好的会看到,未来的时间,什么时候

4

3 回答 3

1

好的,成功了。在论坛上找到以下代码并根据我的需要进行更改,它有点奇怪但有效。在鼠标移动事件中

Public cursorAt As Integer
    Public wordEnd As Integer
    Public isEnd As Boolean
    Public wordStart As Integer
cursorAt = RichTextBox1.GetCharIndexFromPosition(e.Location)
       If cursorAt = 0 Then Return
       wordEnd = New Regex("[\s$]{1}").Match(RichTextBox1.Text.Substring(cursorAt)).Index
       isEnd = Not New Regex("\s").IsMatch(RichTextBox1.Text.Substring(cursorAt))
       wordStart = New Regex("[\s^]{1}", RegexOptions.RightToLeft).Match(RichTextBox1.Text.Substring(0, cursorAt)).Index
       If isEnd Then
 RichTextBox1.Select(wordStart, RichTextBox1.Text.Length)
                  Else
          RichTextBox1.Select(wordStart, cursorAt - wordStart + wordEnd)

       End If

然后简单地替换 then 单词

RichTextBox1.SelectedText = myclickeditem
于 2012-05-11T13:51:45.037 回答
-1

F6.RichTextBox1.Focus() SendKeys.Send(TextBox4.Text)

于 2013-10-04T12:34:08.580 回答
-1

我认为 user934820 建议的代码可以改进。使用“\b”更好地寻找单词边界。作为奖励,我们可以忽略文本结尾的大小写(因为它也是单词边框)。所以代码看起来像

wordEnd = New Regex(".\b{1}").Match(RichTextBox1.Text.Substring(cursorAt)).Index
wordStart = New Regex("\b.{1}", RegexOptions.RightToLeft).Match(RichTextBox1.Text.Substring(0, cursorAt)).Index

RichTextBox1.Select(wordStart, cursorAt - wordStart + wordEnd + 1)

Please, pay attention that this search will return the very last letter in the word NOT the space after it (as in original code). So increment by 1 where needed (like Selection).

于 2016-01-04T15:28:37.780 回答