Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 用户键入时突出显示特定文本_Vb.net_Richtextbox - Fatal编程技术网

Vb.net 用户键入时突出显示特定文本

Vb.net 用户键入时突出显示特定文本,vb.net,richtextbox,Vb.net,Richtextbox,我正在编写一个代码,突出显示文本中重复的单词。当我添加一个按钮时,代码运行良好,用户必须按下按钮以检查重复项 但是我想做一个自动检查代码。我在处理RichTextBox.TextChanged的子例程中设置代码。问题是代码选择目标单词并高亮显示,但选择仍然存在,因此当键入新字母时,会清除高亮显示的内容 这是我的密码: Private Sub RichTextBox_TextChanged(sender As Object, e As EventArgs) Handles RichTextBox.

我正在编写一个代码,突出显示文本中重复的单词。当我添加一个按钮时,代码运行良好,用户必须按下按钮以检查重复项

但是我想做一个自动检查代码。我在处理RichTextBox.TextChanged的子例程中设置代码。问题是代码选择目标单词并高亮显示,但选择仍然存在,因此当键入新字母时,会清除高亮显示的内容

这是我的密码:

Private Sub RichTextBox_TextChanged(sender As Object, e As EventArgs) Handles RichTextBox.TextChanged
        Try

        Call duplicate_check()

    Catch ex As Exception
        MessageBox.Show("error in RichTextBox.TextChanged")
    End Try
End Sub
重复检查功能:

Private Sub duplicate_check()
        Try
            ' read line by line and get input 
            Dim LineByLineInput() As String = RichTextBox.Lines
            Dim selectionStart, selectionLength As Integer
            Dim i, j As Integer

            For lineNumber = 0 To UBound(LineByLineInput)
                selectionStart = 0
                selectionLength = 0
                'get index of first char index in the current line
                Dim count As Integer = lineNumber
                While count <> 0
                    selectionStart += RichTextBox.Lines(count - 1).Length + 1
                    count -= 1
                End While
                ' get line as string
                Dim line As String = RichTextBox.Lines(lineNumber)
                ' split line into array of strings
                Dim input() As String = line.Split(" ")
                'check for duplicates
                i = 0
                For j = i + 1 To UBound(input)

                    If input(i) = input(j) Then 'compare each 2 consecutive words if they are the same
                        selectionStart += input(i).Length + 1
                        selectionLength = input(i).Length
                        RichTextBox.SelectionStart = selectionStart
                        RichTextBox.SelectionLength = selectionLength
                        RichTextBox.SelectionBackColor = Color.Yellow

                    Else
                        selectionStart += input(i).Length + 1
                    End If
                    i += 1
                Next
            Next
        Catch ex As Exception
            MessageBox.Show("error duplicate_check()")
        End Try

    End Sub
私有子副本检查()
尝试
'逐行读取并获取输入
Dim LineByLineInput()作为String=RichTextBox.Lines
Dim selectionStart,selectionLength为整数
作为整数的Dim i,j
对于lineNumber=0,为UBound(LineByLineInput)
selectionStart=0
selectionLength=0
'获取当前行中第一个字符索引的索引
Dim计数为整数=行号
而计数为0
selectionStart+=RichTextBox.Lines(计数-1)。长度+1
计数-=1
结束时
'将行作为字符串获取
按字符串标注行=RichTextBox.Lines(行号)
'将行拆分为字符串数组
Dim input()作为字符串=line.Split(“”)
'检查重复项
i=0
对于j=i+1至UBound(输入)
如果输入(i)=输入(j),则“比较两个连续单词是否相同”
selectionStart+=输入(i)。长度+1
selectionLength=输入(i).长度
RichTextBox.SelectionStart=SelectionStart
RichTextBox.SelectionLength=SelectionLength
RichTextBox.SelectionBackColor=Color.Yellow
其他的
selectionStart+=输入(i)。长度+1
如果结束
i+=1
下一个
下一个
特例
MessageBox.Show(“错误重复检查()”)
结束尝试
端接头

在重复检查呼叫后,您是否尝试将RichTextBox的选择设置回原来的位置

见下文:

 Private Sub RichTextBox1_TextChanged(sender As Object, e As System.EventArgs) Handles RichTextBox1.TextChanged
        Try
            ' Get current position
            Dim cur_pos As Integer = Me.RichTextBox.SelectionStart
            Call duplicate_check()
            ' Set back to old position
            Me.RichTextBox.SelectionStart = cur_pos
            ' Unselect what your sub duplicate_check has selected
            Me.RichTextBox1.SelectionLength = 0

        Catch ex As Exception
            MessageBox.Show("error in RichTextBox.TextChanged")
        End Try
    End Sub

如果此解决方案适合您,您应该更改您的复制检查子项以进行此更改,而不是在RichTextBox1\u TextChanged子项中进行此更改

为什么复制项必须保持选中状态?如果有多个副本呢?只需突出显示文本,让他们不受干扰地继续键入。@a朋友,突出显示文本后如何取消选择文本?看一看“是”,这会产生另一个问题,如果用户将光标移动到特定位置(编辑文本或s.th),光标将再次移动到程序末尾。。谁能帮帮我