Как найти и выделить следующий экземпляр слова в richtextbox в Visual Basic? - PullRequest
0 голосов
/ 12 октября 2018

Вот форма приложения Visual Basic Windows, над которой я сейчас работаю.

https://i.snag.gy/EzoXr3.jpg

Как мне написать код для кнопки поиска следующего, как в этом видео.

https://www.dropbox.com/s/u93h9jn605uhwsu/CPT341Project2Walkthrough.avi?dl=0

Если я смогу получить синтаксис для хранения индексной переменной за пределами частного подпрограммы кнопки findnext, я могу решить эту проблему.Потому что каждый раз, когда я нажимаю кнопку «найти следующий текст», я должен найти соответствующий экземпляр этого слова в текстовом файле.Или я могу применить некоторый код, чтобы найти его в подстроке оставшегося текста после его первого появления.

Ниже приведен мой код:

Public Class Form1
'Enter your name
'Date
'Class - CPT 341 VB.NET NJIT

Private Sub openBtn_Click(sender As Object, e As EventArgs) Handles openBtn.Click
    If OpenFileDialog.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
        fileText.Text = My.Computer.FileSystem.ReadAllText(OpenFileDialog.FileName)
    End If
End Sub

Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
    'Bring the form to the inital state for a different search
    Dim temp As String = fileText.Text
    fileText.Text = temp
    outputIndex.ForeColor = Color.Black

    'MessageBox to show error for empty search textbox
    If inputText.Text = "" Then
        MessageBox.Show("Enter Text to Search", "CPT 341 Fall 2018 - Project 2")
    Else
        'Declare variables
        Dim txt As String : Dim x As Integer = 0 : Dim output As String
        txt = inputText.Text
        If fileText.Text.IndexOf(txt) <> -1 Then
            Dim idx As Integer = CStr(fileText.Text.IndexOf(txt))
            outputIndex.Text = idx

            'Find and highlight the first word searched in the RichTextBox
            fileText.Find(txt, x, fileText.TextLength, RichTextBoxFinds.None)
            fileText.SelectionBackColor = Color.Yellow

            'populate the string with ANSI numbers
            output = Asc(txt(0))
            For x = 1 To (txt.Length - 1)
                output = output & " " & Asc(txt(x))
            Next x
            outputANSI.Text = output

        ElseIf fileText.Text = "" Then
            MessageBox.Show("Please open a file to search from", "CPT 341 Fall 2018 - Project 2")

        Else
            outputIndex.Text = txt & " is not found"
            'Bring the form to inital state
            fileText.Text = temp
            'Change the index textbox text color to red
            outputIndex.ForeColor = Color.Red
            'Empty the ANSI textbox
            outputANSI.Text = ""
        End If
    End If

End Sub

Private Sub btnFind_Click(sender As Object, e As EventArgs) Handles btnFind.Click

    Dim txt As String = inputText.Text
    Dim Index As Integer = fileText.Text.IndexOf(txt) + txt.Length
    'Find and highlight the word searched in the RichTextBox other than first occurrence

    fileText.Find(txt, Index, fileText.TextLength, RichTextBoxFinds.None)
    fileText.SelectionBackColor = Color.Yellow
    Index = fileText.Text.IndexOf(txt, Index) + txt.Length + 1

End Sub

Private Sub btnWords_Click(sender As Object, e As EventArgs) Handles btnWords.Click
    wordCount.Text = fileText.Text.Split(" ").Length
End Sub

Private Sub btnChars_Click(sender As Object, e As EventArgs) Handles btnChars.Click
    charCount.Text = fileText.Text.Length
End Sub
End Class

Или любое другое предложение будет полезным.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...