none
I would like to add Previous Button for my form,however it gives me error.Kindly help me RRS feed

  • Question

  • Private findPos As Integer = 0
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            Dim searchText As String = txtFinds.Text

            Try
                Dim s As String = txtFinds.Text
                RichTextBox1.Focus()
                findPos = RichTextBox1.Find(s, findPos, RichTextBoxFinds.None)
                RichTextBox1.[Select](findPos, s.Length)


                findPos += txtFinds.Text.Length + 1


            Catch
                MsgBox("No Matched text Found", MsgBoxStyle.OkOnly, "Invalid")
                findPos = 0
            End Try
        End Sub

     Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
            Dim searchText As String = txtFinds.Text

            Try
                Dim t As String = txtFinds.Text
                RichTextBox1.Focus()
                findPos = RichTextBox1.Find(t, findPos, RichTextBoxFinds.WholeWord)
                RichTextBox1.[Select](findPos, t.Length)
                findPos -= txtFinds.Text.Length


            Catch
                MsgBox("No Matched text Found", MsgBoxStyle.OkOnly, "Invalid")
                findPos = 0
            End Try

        End Sub

                
    • Edited by Gh000st Friday, August 7, 2020 7:02 AM
    Friday, August 7, 2020 6:58 AM

Answers

  • Hi Gh000st,

    Thank you for posting here.

    I make a test on my side and then I use following code to get 'Find Previous' and 'Find Next' functionality in my RichTextBox:

        Private findPos As Integer = 0
        Private setPos As HashSet(Of Integer) = New HashSet(Of Integer)
        Dim index As Integer = 0
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            If (OpenFileDialog1.ShowDialog = DialogResult.OK) Then
                RichTextBox1.Text = My.Computer.FileSystem.ReadAllText(OpenFileDialog1.FileName)
            Else
                MsgBox("Not Found")
            End If
        End Sub
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            Try
                Dim s As String = TextBox1.Text
                RichTextBox1.Focus()
                If setPos.Contains(findPos) Then
                    If Not index - 1 < 0 Then
                        index -= 1
                    End If
                    findPos += TextBox1.Text.Length
                    findPos = RichTextBox1.Find(s, findPos, RichTextBoxFinds.None)
                    If Not setPos.Contains(findPos) Then
                        setPos.Add(findPos)
                    End If
                    RichTextBox1.[Select](findPos, s.Length)
                Else
                    findPos = RichTextBox1.Find(s, findPos, RichTextBoxFinds.None)
                    setPos.Add(findPos)
                    RichTextBox1.[Select](findPos, s.Length)
                    findPos += TextBox1.Text.Length
                End If
            Catch
                MessageBox.Show("No Matched text Found")
                findPos = 0
                setPos.Clear()
            End Try
        End Sub
        Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
            Dim s As String = TextBox1.Text
            RichTextBox1.Focus()
            index += 1
            findPos = RichTextBox1.Find(s, setPos(setPos.Count - 1 - index), RichTextBoxFinds.None)
            RichTextBox1.[Select](findPos, s.Length)
        End Sub

    Result of my test:

    Hope it could be helpful.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, August 10, 2020 3:33 AM
    Moderator