在richtextbox vb.net中的鼠标下替换当前单词 [英] replace current word under mouse in richtextbox vb.net

查看:135
本文介绍了在richtextbox vb.net中的鼠标下替换当前单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在用户单击contextmenustrip dorpdownitem时用鼠标光标替换当前单词,就像MS Office word中的同义词选项一样.我有要替换的词和有替换词.但我无法获取我要替换的单词的确切位置.这是代码.

 私有  tsmmutradifat__DropDownItemClicked( ByVal 发​​件人 As  对象 ByVal  e  As  System.Windows.Forms.ToolStripItemClickedEventArgs)句柄 tsmmutradifat_.DropDownItemClicked
     Dim  myclickeditem  As  字符串 = correct_word(e .ClickedItem.Text)
     Dim 替换单词 As  字符串
    如果 不是 myclickeditem = 什么都没有 然后
        如果 RichTextBox1.SelectedText = "  然后
            ' 所需代码
        其他
            wordtoreplace = RichTextBox1.SelectedText
            如果将单词替换掉.EndsWith(" ) 然后
                myclickeditem = myclickeditem& " 
            结束 如果
            如果 wordtoreplace.StartsWith(" ) 然后
                myclickeditem = " & myclickeditem
            结束 如果
            RichTextBox1.SelectedText = myclickeditem
        结束 如果
    结束 如果
结束  

解决方案

ok在论坛上找到以下代码,并根据我的需要对其进行了更改,但有些奇怪,但可以正常工作.
用鼠标移动envent

 公共 cursorAt  As  整数
    公共 wordEnd  As  整数
    公共 isEnd  As  布尔值
    公共 wordStart  As  整数
cursorAt = RichTextBox1.GetCharIndexFromPosition(e.Location)
        If  cursorAt =  0  然后 返回
       wordEnd = 新建正则表达式(" ).Match(RichTextBox1.Text.Substring(cursorAt)).Index
       isEnd = 不是  Regex("  \ s").IsMatch(RichTextBox1.Text.Substring(cursorAt))
       wordStart = 新建正则表达式(" ,RegexOptions.RightToLeft).Match(RichTextBox1.Text.Substring(如果 isEnd 然后
 RichTextBox1.选择(wordStart,RichTextBox1.Text.Length)
                  其他
          RichTextBox1.选择(wordStart,cursorAt-wordStart + wordEnd)
          
       结束 如果 



然后只需替换单词

 RichTextBox1.SelectedText = myclickeditem 


i want to replace the current word under mouse cursor when user click on contextmenustrip dorpdownitem just like synonymous option in MS office word. i have word to replace and word for replace. but iam unable to get the exact location of word i want to replace. here is the code.

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

解决方案

ok made find the below code on a forum and changed it according to my need it a little odd but working.
In mouse move envent

    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



and then simply replace then word

RichTextBox1.SelectedText = myclickeditem


这篇关于在richtextbox vb.net中的鼠标下替换当前单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆