Click here to Skip to main content
15,748,477 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hello Code Community, This is my first time posting. I hope this makes sense.
I am using Visual Studio 2005.

I am working on a form that basically loads file of data and the user is allowed to edit the data in many ways. I am having trouble adding SEARCH TEXT tool to the application.

I have one form called frmMain. It contains a big Rich Text Box. I have created buttons to open and close data files (most of them are normal txt files). I have a button named btnSearch. When clicked a new form named frmSearch pops up. This form has a textbox for the user to enter the text they wish to search for. Then when they press the Search button, it is supposed to search the file loaded into frmMain for the entered text and highlight it. Much like Microsoft Word or a search function in Firefox or IE.

My problem is that when the search takes place the first instance of the text is highlighted, but if there are more of the texts in the file, the screen or page does not move down for the user to see the other instances. If there are 5 instances of the text JONES, it will find all 5 on each click of the search button, but the user can only see the ones that are already on the screen.

How can I make it so that if there are instances of the text in pages below, the page move according to the instance of the text that is found.

MY CODE on frmSearch:

Private Sub btnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFind.Click
        If String.IsNullOrEmpty(txtSearch.Text) Then
            MessageBox.Show("No text to search")
            SearchFunction(frmMain.rtb.Text.ToLower, txtSearch.Text.ToLower)
        End If
    End Sub

    Dim StartIndex As Integer
    Private Sub SearchFunction(ByVal Text As String, ByVal SearhText As String)
        StartIndex = Text.IndexOf(SearhText, StartIndex + SearhText.Length)

        If StartIndex <> -1 Then
            frmMain.rtb.Select(StartIndex, SearhText.Length)
            MessageBox.Show("Designer has finished searching the data")
            StartIndex = 0
        End If
    End Sub
End Class

Again I hope this makes sense to the community, I apologize if the problems isn't clear. I tried to explain it as best as possible. Any help or comments is much appreciated. Thank You.
cuteband 19-May-11 21:48pm    
Does your rich texbox have a vertical scroll bar?
Yibs317 20-May-11 19:29pm    
YEs it does.

1 solution

Are you using WPF, or Windows Forms? If it is Windows Forms, there is a method in the RichTextBox class, inherited from TextBoxBase, that might do the trick:

Take a look at it.
Share this answer
Yibs317 23-May-11 12:50pm    
Thanks for the info. This helped a little. Now when I search; the text that is searched is always at the top of the screen. So it is moving throughout the pages of text, which is much more useful than what it did before. However the text is not being highlighted anymore. Any ideas? From my code above I added "frmMain.rtb.ScrollToCaret()" after the "frmMain.rtb.Select()" line.
[no name] 23-May-11 14:14pm    
The text not being highlighted might have something to do with focus... have you tried setting the HideSelection property of the RichTextBox to false?
Yibs317 24-May-11 21:00pm    
This worked brilliantly. Thanks for all the help

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900