1

我正在使用visual basic,我正在创建一个函数来搜索输入到richtextbox 中的每个单词。当一个词被输入到RichTextBox2搜索中时RichTextBox1,文本被突出显示。

RichTextBox1并且RiochTextBox2是只读的,RichTextBox2可以通过代码写入,而RichTextBox1只有默认文本(单词目录)。也有RichTextBox3其中持有RichTextBox2's文本的副本

RichTextBox3.Text = RichTextBox2.Text

这是函数的代码。

public class textsearch
Private intPosition As Integer

Private Sub NextButton_Click(sender As Object, e As EventArgs) Handles NextButton.Click
    generatekanji()

    '   Static intStart As Integer
    'used to select compare method
    Dim intStart As Integer
    Dim objType As Object
    Dim lastWord As String = RichTextBox2.Text.Split(" ").Last
    objType = CompareMethod.Text

    'set starting position to 1
    intPosition = 1
    'use the InStr function to look up a staring position of a search string in a given text box using objType (case-insensitive or case-sensitive)
    intStart = InStr(intPosition, RichTextBox1.Text, lastWord, objType) ' what it searches
    If intStart > 0 Then

        'set starting select position on a textbox and select the search string
        RichTextBox1.SelectionStart = intStart - 1
        RichTextBox1.SelectionLength = lastWord.Length 'highlights the searched word
        RichTextBox1.Select()

    End If

End Sub
End Class

这是一个非常有用的功能,但主要问题是

(1) 因为RichTextBox1是只读的,所以每次执行搜索时都会发出“叮”的声音,这很烦人。

(2) I am unable to find a way to select the character whenever a word is found , or how to replace the word in RichTextBox3 with the character next to the searched word.

Could someone help with this problem .

4

1 回答 1

0

When I use this code the search word is highlighted with no dinging, in a readonly richtextbox:

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim SearchWord As String = RichTextBox2.text
    Dim SelStart As Integer = InStr(RichTextBox1.Text, SearchWord, CompareMethod.Text)
    If SelStart > 0 Then
        RichTextBox1.Select(SelStart - 1, SearchWord.Length)
        RichTextBox1.Focus()
        For Each line In RichTextBox1.Lines
            If line.Contains(SearchWord) Then
                RichTextBox3.Text = line.Split()(0)
            End If
        Next
    End If
End Sub

I'm wondering if the dinging is coming from the generatekanji() routine.

于 2013-06-02T04:01:08.290 回答