How do I create a Find Next button for a Find and Replace?

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

56 Replies - 3364 Views - Last Post: 16 November 2012 - 12:18 PM Rate Topic: -----

#1 James1992  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 130
  • Joined: 30-October 12

How do I create a Find Next button for a Find and Replace?

Posted 13 November 2012 - 03:15 AM

Right, I'm currently creating a Find and Replace form. This form should function exactly like the one found within Microsoft Word. As a result, a user will be able to search for a string of text within a document or the selected text.

I was originally using this code.

Dim selectedTextStart As Integer

        selectedTextStart = InStr(form1.RichTextBox.Text, textToFind.Text)

        If selectedTextStart = 0 Then
            MessageBox.Show("Show some warning text")
            Exit Sub
        End If
        form1.RichTextBox.Select(selectedTextStart - 1, textToFind.Text.Length)
        form1.RichTextBox.ScrollToCaret()



I was informed by a user on dreamincode that I should scrap the InStr function and instead use something like this:

Dim lastPosition As Integer = 0
lastPosition = richTextBox.Find(textToFind, lastPosition, form1.MatchCase) 
'MatchCase or whatever search you do)
last position = last position  + 1



The above code worked fine to some extent. However I then realised it was missing some functionality such as:

How do I allow the user to only perform the 'Find and Replace' on only some text that they selected?

How do I allow the user to change the starting position of the 'Find and Replace'? In other words, once they click the Find button and the first instance has happened, what if they want to move the cursor back to the beginning... how would the code enable them to move the cursor?

Would be much help if you could assist me.

Thank you.

Is This A Good Question/Topic? 0
  • +

Replies To: How do I create a Find Next button for a Find and Replace?

#2 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 769
  • Joined: 19-October 11

Re: How do I create a Find Next button for a Find and Replace?

Posted 13 November 2012 - 04:34 AM

RichTextBox has Selectionstart and SelectionLenght properties. Use them to limit the scope of searching.
Was This Post Helpful? 0
  • +
  • -

#3 James1992  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 130
  • Joined: 30-October 12

Re: How do I create a Find Next button for a Find and Replace?

Posted 13 November 2012 - 05:09 AM

k

This post has been edited by James1992: 13 November 2012 - 05:20 AM

Was This Post Helpful? 0
  • +
  • -

#4 James1992  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 130
  • Joined: 30-October 12

Re: How do I create a Find Next button for a Find and Replace?

Posted 13 November 2012 - 07:11 AM

I still can't figure this one out.

This is my code

 dim lastPosition As Integer = 0


lastPosition = RichTextBox.Find(textToFind, lastPosition, RichTextBoxFinds.MatchCase)

form1.RichTextBox.Selectionstart=lastPosition
form1.RichTextBox.SelectionLength=textToFind.text.Length

lastPosition=lastPosition+1




Problem 1: What if the user decides to change the position of the select cursor(the blinking line)? I need to make my code adapt to this so that it continues from where the cursor currently is. Because at the moment, if the user changes the select cursor, it still continues from the last position.

Problem 2: What if the user only wants to apply the Find and Replace to a paragraph and not the whole document? How would I include this...

Thank you for any help!

This post has been edited by James1992: 13 November 2012 - 07:12 AM

Was This Post Helpful? 0
  • +
  • -

#5 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 769
  • Joined: 19-October 11

Re: How do I create a Find Next button for a Find and Replace?

Posted 13 November 2012 - 07:24 AM

1: set position to Selectionstart in Selectionchanged event handler.
2: at the same event, set the limits for searching between Selectionstart to SelectionLenght.
Was This Post Helpful? 0
  • +
  • -

#6 James1992  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 130
  • Joined: 30-October 12

Re: How do I create a Find Next button for a Find and Replace?

Posted 13 November 2012 - 07:40 AM

View Postlucky3, on 13 November 2012 - 07:24 AM, said:

1: set position to Selectionstart in Selectionchanged event handler.
2: at the same event, set the limits for searching between Selectionstart to SelectionLenght.



So my code would be something like this?

 Private Sub RichTextBox_TextChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox.Selectionchanged
        frm2.lastposition = RichTextBox.Selectionstart

    End Sub



How would I do the limits?
Was This Post Helpful? 0
  • +
  • -

#7 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 769
  • Joined: 19-October 11

Re: How do I create a Find Next button for a Find and Replace?

Posted 13 November 2012 - 08:15 AM

You'll have to take into account several things:
  • if no text is selected by user, search whole text in RTB;
  • on text changed, you'll want to change value of end position variable, which you'll use to limit the scope of searching for user entered text;
  • when you'll find first matched string within the scope of text, where you are searching, you'll want to adjust value of where to start searching for next match


There are several things to do, before you'll reach your goal.

This post has been edited by lucky3: 13 November 2012 - 08:16 AM

Was This Post Helpful? 0
  • +
  • -

#8 James1992  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 130
  • Joined: 30-October 12

Re: How do I create a Find Next button for a Find and Replace?

Posted 13 November 2012 - 08:24 AM

View Postlucky3, on 13 November 2012 - 08:15 AM, said:

You'll have to take into account several things:
  • if no text is selected by user, search whole text in RTB;
  • on text changed, you'll want to change value of end position variable, which you'll use to limit the scope of searching for user entered text;
  • when you'll find first matched string within the scope of text, where you are searching, you'll want to adjust value of where to start searching for next match


There are several things to do, before you'll reach your goal.


So when the first matched string within the scope of text is found and as a result is selected, this won't alter the stored information about the text that was initially selected?



Right, had a little crack at this.

I've set a public boolean called notSelected on form1 (where the RTB is) and it is initially set to false.

In the selectionchanged event handler for the RTB i have done this:

 dim scope as string
if notSelected = false then
scope = RichTextBox.SelectionLength
notSelected = true
Else
scope = scope - form2.lastposition



Now I have already faced a problem with this, soon as the user begins typing in the RTB, it sets the boolean to true, rather than waiting until the user has selected some text.

Am I on the right track? :/

This post has been edited by James1992: 13 November 2012 - 08:56 AM

Was This Post Helpful? 0
  • +
  • -

#9 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 769
  • Joined: 19-October 11

Re: How do I create a Find Next button for a Find and Replace?

Posted 13 November 2012 - 09:39 AM

Part of programming is planing and finding the solution. I would start with pseudo-code scenarios:
Scenario 1 - User doesn't select text when searches for matches:
- program start (add here what global variables would you need)
- user enters some text in rtbTextEditor
- user enters some text to be found in txtSearchThisText
- user clicks on search button btnSearch
- first match is selected in rtbTextEditor
- user clicks on btnSearch again
- next match is selected in rtbTextEditor

This is very rough, and if you think will do what you need, go into more details. Something like:
- program start
- set startPosition to 0, endPosition to 0
- user enters some text in rtbTextEditor
- set endPosition to rtbTextEditor.TextLength
- user enters some text to be found in txtSearchThisText
- user clicks on search button btnSearch
- start searching from startPosition until match is found or endPosition reached
- first match is selected in rtbTextEditor
- set startPosition to the last character of found match
- user clicks on btnSearch again
- repeat procedure from the last 4 steps
- next match is selected in rtbTextEditor

etc... I'm not saying this is how it should be, and is flawless, it's just example. Then do something similar in case user selects some text, and app should search in selected text only. Try to join those two scenarios after that, and when you'll have the flow done, start coding.
Was This Post Helpful? 0
  • +
  • -

#10 James1992  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 130
  • Joined: 30-October 12

Re: How do I create a Find Next button for a Find and Replace?

Posted 13 November 2012 - 10:42 AM

View Postlucky3, on 13 November 2012 - 09:39 AM, said:

Part of programming is planing and finding the solution. I would start with pseudo-code scenarios:
Scenario 1 - User doesn't select text when searches for matches:
- program start (add here what global variables would you need)
- user enters some text in rtbTextEditor
- user enters some text to be found in txtSearchThisText
- user clicks on search button btnSearch
- first match is selected in rtbTextEditor
- user clicks on btnSearch again
- next match is selected in rtbTextEditor

This is very rough, and if you think will do what you need, go into more details. Something like:
- program start
- set startPosition to 0, endPosition to 0
- user enters some text in rtbTextEditor
- set endPosition to rtbTextEditor.TextLength
- user enters some text to be found in txtSearchThisText
- user clicks on search button btnSearch
- start searching from startPosition until match is found or endPosition reached
- first match is selected in rtbTextEditor
- set startPosition to the last character of found match
- user clicks on btnSearch again
- repeat procedure from the last 4 steps
- next match is selected in rtbTextEditor

etc... I'm not saying this is how it should be, and is flawless, it's just example. Then do something similar in case user selects some text, and app should search in selected text only. Try to join those two scenarios after that, and when you'll have the flow done, start coding.




Argh, had another go and I can't get my head around it!!

I've created a public boolean in frm2 called intInitialSelection

In the form load of frm2 i have set the boolean to false.

Within the find next click event I have done this:

If intSelectionstart = False Then
            If frm1.RichTextBox.SelectedText > 0 Then
                scope = frm1.RichTextBox.SelectedText.Length
            else
            scope = frm1.RichTextBox.Text.Length
end if
'then do following code...
intSelectionstart = true

'So what the above code is doing, is determing what value to assign to the variable 'Scope'. That is, whether to set it 'to the length of the selected text or the whole text.


Else
            If lastposition <= scope Then
'do following code..
Else
                MessageBox.Show("Reached limit")
            End If
        End If



However, this does not work.

Spare some help?

This post has been edited by James1992: 13 November 2012 - 10:43 AM

Was This Post Helpful? 0
  • +
  • -

#11 James1992  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 130
  • Joined: 30-October 12

Re: How do I create a Find Next button for a Find and Replace?

Posted 13 November 2012 - 10:47 AM

When text is selected, I am unable to enter text into the textToFind textbox.

I have deselect text first, then type in textToFind, then select the text. However even then I get an error saying "Conversion from string "new new" to type 'Double' is not valid."
Was This Post Helpful? 0
  • +
  • -

#12 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 769
  • Joined: 19-October 11

Re: How do I create a Find Next button for a Find and Replace?

Posted 13 November 2012 - 11:12 AM

Here is example of how you could approach the problem:

Friend Class SearchMatches
    Public ReadOnly Property SearchPhrase As String
        Get
            Return _phrase
        End Get
    End Property
    Public ReadOnly Property Matches As List(Of Integer)
        Get
            Return _matches
        End Get
    End Property

    Private _phrase As String
    Private _matches As New List(Of Integer)
    Private _lastListIndex As Integer
    Private _startIndex As Integer
    Private _endIndex As Integer
    Private _text As String

    ''' <summary>
    ''' Finds all positions of matched string in text.
    ''' </summary>
    ''' <param name="startIndex">Where in searchedText should search start.</param>
    ''' <param name="endIndex">Where in searchedText should search end.</param>
    ''' <param name="searchedText">Whole text from where we will find all phrase matches.</param>
    ''' <param name="searchPhrase">Phrase we are searching for in searchedText.</param>
    ''' <remarks></remarks>
    Public Sub New(startIndex As Integer, endIndex As Integer, searchedText As String, searchPhrase As String)
        _startIndex = startIndex
        _endIndex = endIndex
        _text = searchedText
        _phrase = searchPhrase
    End Sub

    Public Sub FindMatches()
        'write your logic here
    End Sub
End Class



Note: you could construct such class in various different ways. You could have FindMatches function instead of sub, you perhaps wouldn't need Matches public property....
Was This Post Helpful? 0
  • +
  • -

#13 James1992  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 130
  • Joined: 30-October 12

Re: How do I create a Find Next button for a Find and Replace?

Posted 13 November 2012 - 11:28 AM

That looks more complicated then I was hoping :/ thank you anyway!
Was This Post Helpful? 0
  • +
  • -

#14 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 769
  • Joined: 19-October 11

Re: How do I create a Find Next button for a Find and Replace?

Posted 13 November 2012 - 11:35 AM

Perhaps it *looks*, but it really isn't. I'd say, it's much easier then spaghetti code. It's just separation of concern. Why don't you give it a try?
Was This Post Helpful? 0
  • +
  • -

#15 James1992  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 130
  • Joined: 30-October 12

Re: How do I create a Find Next button for a Find and Replace?

Posted 13 November 2012 - 12:15 PM

Because I have no idea what it's doing. I'll have a crack at it tomorrow. Thank you!
Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »