Results 1 to 10 of 10

Thread: Find Text: Previous

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2011
    Posts
    39

    Question Find Text: Previous

    I'm making a notepad program and i want the user to be able to search for text. I got the code working for the "Find Next" button, but I cant get the code for the "Find Previous" button.

    Here's what I have for Find Next:
    Code:
    Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
    
            Dim x As Integer
            Dim opt As RichTextBoxFinds = 0
            If chkCase.Checked Then opt = opt Or RichTextBoxFinds.MatchCase
            x = CType(frmMain.TabControl.SelectedTab.Controls.Item(0), RichTextBox).Find(txtFind.Text, PlaceHolder, opt)
            If x < 0 Then
                If MessageBox.Show("There are no more occurances of " & txtFind.Text & vbCrLf & "Start at begining?", "Can't Find", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) = Windows.Forms.DialogResult.Yes Then
                    PlaceHolder = 0
                    btnNext.PerformClick()
                End If
            End If
            PlaceHolder = CType(frmMain.TabControl.SelectedTab.Controls.Item(0), RichTextBox).SelectionStart + 1
            CType(frmMain.TabControl.SelectedTab.Controls.Item(0), RichTextBox).Focus()
    
        End Sub
    Here's what I have for Find Previous:
    I just can't seem to make it work. I've been messing with it and trying all sorts of different options, it just keeps messing up.
    Code:
    Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
            Dim x As Integer
            Dim opt As RichTextBoxFinds = 0
            If chkCase.Checked Then opt = opt Or RichTextBoxFinds.MatchCase
            x = CType(frmMain.TabControl.SelectedTab.Controls.Item(0), RichTextBox).Find(txtFind.Text, PlaceHolder, opt)
            If x < 0 Then
                If MessageBox.Show("There are no more occurrences of " & txtFind.Text & vbCrLf & "Start at begining?", "Word or Phrase Not Found", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) = Windows.Forms.DialogResult.Yes Then
                    PlaceHolder = 0
                    btnNext.PerformClick()
                End If
            End If
            PlaceHolder = CType(frmMain.TabControl.SelectedTab.Controls.Item(0), RichTextBox).SelectionStart - 1
            CType(frmMain.TabControl.SelectedTab.Controls.Item(0), RichTextBox).Focus()
    
        End Sub
    Any help is greatly appreciated!

    ~Thanks in advance

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: Find Text: Previous

    vb Code:
    1. Dim startAt As Integer = 'the position in your text to start from
    2. x = RichTextBox1.Find("a", startAt, RichTextBoxFinds.Reverse)

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2011
    Posts
    39

    Re: Find Text: Previous

    I already tried that, it doesn't work at all. No matter what the start position is, it always starts at the bottom. And for some reason, it doesn't move up.

  4. #4
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Find Text: Previous

    Your options are stored in a variable of type RichTextBoxFinds named opt. Let's see how it's set up in both of your methods.

    Find next:
    Code:
    Dim opt As RichTextBoxFinds = 0
    If chkCase.Checked Then opt = opt Or RichTextBoxFinds.MatchCase
    So in this case it does whatever 0, or 0 OR MatchCase does. You have to turn to disassembly to know what that is for sure; in this case you get 0 or 4; None or MatchCase.

    Find previous:
    Code:
    Dim opt As RichTextBoxFinds = 0
    If chkCase.Checked Then opt = opt Or RichTextBoxFinds.MatchCase
    Oh dear. It's the same. Perhaps you should try .paul.'s advice, or at least produce the code you have that isn't working so we can determine if there's another problem.

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: Find Text: Previous

    try this:

    vb Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.     Dim endAt As Integer = RichTextBox1.Text.Length
    3.     endAt = RichTextBox1.Find("it", 0, endAt, RichTextBoxFinds.Reverse) - 1
    4.     Do While endAt > -1
    5.         RichTextBox1.SelectionFont = New Font(RichTextBox1.Font, FontStyle.Bold)
    6.         endAt = RichTextBox1.Find("it", 0, endAt, RichTextBoxFinds.Reverse) - 1
    7.     Loop
    8. End Sub

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: Find Text: Previous

    the problem with the startAt parameter is if you set it to 100 it'll reverse search from the end of the text to startAt. startAt needs to be 0 + the endAt parameter needs to be 1 char before the last find, then that's where it'll reverse search from...

  7. #7

    Thread Starter
    Member
    Join Date
    Mar 2011
    Posts
    39

    Re: Find Text: Previous

    I still can't get it to work. All it does is highlight the last occurrence every time I hit the button. Here's the new code:

    Code:
    Dim x As Integer
            Dim opt As RichTextBoxFinds = 0
            If chkCase.Checked Then opt = opt Or RichTextBoxFinds.MatchCase
    
            Dim endAt As Integer = CType(frmMain.TabControl.SelectedTab.Controls.Item(0), RichTextBox).Text.Length
            x = CType(frmMain.TabControl.SelectedTab.Controls.Item(0), RichTextBox).Find(txtFind.Text, 0, endAt, RichTextBoxFinds.Reverse) - 1
            If x < 0 Then
                If MessageBox.Show("There are no more occurrences of " & txtFind.Text & vbCrLf & "Start at begining?", "Word or Phrase Not Found", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) = Windows.Forms.DialogResult.Yes Then
                    PlaceHolder = 0
                    btnNext.PerformClick()
                End If
            End If
    I tried copying your code and I couldn't make it work. I want it to start where the 'find next' left off and just move up. I want it to act almost exactly like the find function in windows notepad, except instead of checking 'up' or 'down' - hit next or previous.

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: Find Text: Previous

    it highlights only the last occurrence because you aren't looping

    vb Code:
    1. Dim x As Integer
    2. Dim opt As RichTextBoxFinds = RichTextBoxFinds.Reverse
    3. If chkCase.Checked Then opt = opt Or RichTextBoxFinds.MatchCase
    4.  
    5. Dim endAt As Integer = CType(frmMain.TabControl.SelectedTab.Controls.Item(0), RichTextBox).Text.Length
    6. endAt = CType(frmMain.TabControl.SelectedTab.Controls.Item(0), RichTextBox).Find(txtFind.Text, 0, endAt, opt) - 1
    7. Do While endAt > -1
    8.     endAt = CType(frmMain.TabControl.SelectedTab.Controls.Item(0), RichTextBox).Find(txtFind.Text, 0, endAt, opt) - 1
    9.     If x < 0 Then
    10.         If MessageBox.Show("There are no more occurrences of " & txtFind.Text & vbCrLf & "Start at begining?", "Word or Phrase Not Found", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) = Windows.Forms.DialogResult.Yes Then
    11.             PlaceHolder = 0
    12.             btnNext.PerformClick()
    13.         End If
    14.     End If
    15. Loop

  9. #9

    Thread Starter
    Member
    Join Date
    Mar 2011
    Posts
    39

    Re: Find Text: Previous

    But because that code loops, it reads from the bottom until it reaches the top doesn't it?

    I need it to stop and highlight the occurrence before the present one. So if I had:

    abc
    abc
    abc
    abc

    I hit 'find next' 3 times and the third one down is highlighted. When I hit 'find previous' I want the second one down to be highlighted. So I changed the code to this:
    vb Code:
    1. Dim x As Integer
    2.         Dim opt As RichTextBoxFinds = RichTextBoxFinds.Reverse
    3.         If chkCase.Checked Then opt = opt Or RichTextBoxFinds.MatchCase
    4.         Dim endAt As Integer = CType(frmMain.TabControl.SelectedTab.Controls.Item(0), RichTextBox).Text.Length
    5.         endAt = CType(frmMain.TabControl.SelectedTab.Controls.Item(0), RichTextBox).Find(txtFind.Text, 0, endAt, opt) - 1
    6.         Do While endAt > PlaceHolder - 1
    7.             endAt = CType(frmMain.TabControl.SelectedTab.Controls.Item(0), RichTextBox).Find(txtFind.Text, 0, endAt, opt) - 1
    8.             If x < 0 Then
    9.                 If MessageBox.Show("There are no more occurrences of " & txtFind.Text & vbCrLf & "Start at begining?", "Word or Phrase Not Found", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) = Windows.Forms.DialogResult.Yes Then
    10.                     PlaceHolder = 0
    11.                     btnNext.PerformClick()
    12.                 End If
    13.             End If
    14.         Loop
    15.         CType(frmMain.TabControl.SelectedTab.Controls.Item(0), RichTextBox).Focus()

    But now it just highlights the same one as the 'find next' button does.

    Thank you by the way for all the help. I'm much closer now than I was before!

  10. #10

    Thread Starter
    Member
    Join Date
    Mar 2011
    Posts
    39

    Re: Find Text: Previous

    Ok this code works almost the way I want it. After you hit the button once, it re-highlights the text-- then after you hit it again it moves up.

    vb Code:
    1. On Error Resume Next
    2.  
    3.         Dim x As Integer
    4.         Dim opt As RichTextBoxFinds = RichTextBoxFinds.Reverse
    5.         If chkCase.Checked Then opt = opt Or RichTextBoxFinds.MatchCase
    6.         Dim endAt As Integer = CType(frmMain.TabControl.SelectedTab.Controls.Item(0), RichTextBox).Text.Length
    7.         endAt = CType(frmMain.TabControl.SelectedTab.Controls.Item(0), RichTextBox).Find(txtFind.Text, 0, endAt, opt) - 1
    8.         Do While endAt > PlaceHolder - 2
    9.             endAt = CType(frmMain.TabControl.SelectedTab.Controls.Item(0), RichTextBox).Find(txtFind.Text, 0, endAt, opt) - 1
    10.             If x < 0 Then
    11.                 If MessageBox.Show("There are no more occurrences of " & txtFind.Text & vbCrLf & "Start at begining?", "Word or Phrase Not Found", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) = Windows.Forms.DialogResult.Yes Then
    12.                     PlaceHolder = 0
    13.                     btnNext.PerformClick()
    14.                 End If
    15.             End If
    16.         Loop
    17.         PlaceHolder -= 3
    18.         CType(frmMain.TabControl.SelectedTab.Controls.Item(0), RichTextBox).Focus()

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width