7 Replies - 6295 Views - Last Post: 07 November 2012 - 07:16 AM Rate Topic: -----

#1 James1992   User is offline

  • D.I.C Head

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

How to do a "Find Next" button in a find and replace function?

Posted 06 November 2012 - 09:04 AM

I currently have a 2 forms. Form1 has a rich text box. Form2 has 2 combo box's (find and replace)

On form2 I have the buttons, find next, replace, replace all.

I have been able to do the 'replace all' button using the follow code.

 Private Sub ReplaceAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ReplaceAll.Click
        Dim testString As String = form1.RichTextBox.Text

        form1.RichTextBox.Text = Replace(testString, find.Text, replaceall.Text)
    End Sub


Now if I am able to work out how to do the "Find next", I will be able to do the replace button by just replacing the selected text.

So let's say the richtextbox on form1 has the sentence, "it's a goodbye from him and it's a goodbye from me"

Now the user types into the find combo box "goodbye" and types into the replace combo box "hello".

I was wondering if it's possible to add index's to each instance of the word goodbye? So that when the find next button is pressed, the index is increased by one and as a result selects the second "goodbye".

Is it possible or is there a better solution? I understand you won't give me the code straight up, so any keywords that will help me will be of much use!

This post has been edited by James1992: 06 November 2012 - 09:05 AM


Is This A Good Question/Topic? 0
  • +

Replies To: How to do a "Find Next" button in a find and replace function?

#2 lar3ry   User is offline

  • Coding Geezer
  • member icon

Reputation: 314
  • View blog
  • Posts: 1,296
  • Joined: 12-September 12

Re: How to do a "Find Next" button in a find and replace function?

Posted 06 November 2012 - 09:19 AM

If I explain it fully, it's about the same as giving you the code, so here's how I did it, in order to highlight all the instances of the search term:

        Dim srchterm As String = "test"
        Dim index As Int32 = 0
        While index <> -1
            index = RichTextBox1.Find(srchterm, index, RichTextBoxFinds.None)
            If index <> -1 Then
                ' do your own thing here.
                RichTextBox1.SelectionBackColor = Color.Orange
                index += 1
            End If
        End While
        RichTextBox1.DeselectAll()


Was This Post Helpful? 0
  • +
  • -

#3 James1992   User is offline

  • D.I.C Head

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

Re: How to do a "Find Next" button in a find and replace function?

Posted 06 November 2012 - 09:36 AM

View Postlar3ry, on 06 November 2012 - 09:19 AM, said:

If I explain it fully, it's about the same as giving you the code, so here's how I did it, in order to highlight all the instances of the search term:

        Dim srchterm As String = "test"
        Dim index As Int32 = 0
        While index <> -1
            index = RichTextBox1.Find(srchterm, index, RichTextBoxFinds.None)
            If index <> -1 Then
                ' do your own thing here.
                RichTextBox1.SelectionBackColor = Color.Orange
                index += 1
            End If
        End While
        RichTextBox1.DeselectAll()



I've already been able to do that using my own code.

I'm after just selecting the first instance of that word. For example, if you press find next, it selects the first word that matches the specified word in the findcombo box. If you press find next again, then it then selects the second word that matches and so on and so on..
Was This Post Helpful? 0
  • +
  • -

#4 James1992   User is offline

  • D.I.C Head

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

Re: How to do a "Find Next" button in a find and replace function?

Posted 06 November 2012 - 09:58 AM

anyone?
Was This Post Helpful? 0
  • +
  • -

#5 lar3ry   User is offline

  • Coding Geezer
  • member icon

Reputation: 314
  • View blog
  • Posts: 1,296
  • Joined: 12-September 12

Re: How to do a "Find Next" button in a find and replace function?

Posted 06 November 2012 - 10:19 AM

View PostJames1992, on 06 November 2012 - 10:36 AM, said:

I've already been able to do that using my own code.

I'm after just selecting the first instance of that word. For example, if you press find next, it selects the first word that matches the specified word in the findcombo box. If you press find next again, then it then selects the second word that matches and so on and so on..

Well, if you do that in your own code, then you already have a solution. I didn't actually spell it out for you, but here it is.

Write this as a subroutine, and call it when you click Find. Where I put the comment ' do your own thing here., save your index values in an array or a list, and after that subroutine returns, go to the first occurrence (using the index already found, if you like).

Keeping track of which index you are currently using, go to the next one when you click Find Next. When you run out of index values, wrap around and do it again.

It's likely only one of several ways to do it, but I think it's a good one.
Was This Post Helpful? 0
  • +
  • -

#6 James1992   User is offline

  • D.I.C Head

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

Re: How to do a "Find Next" button in a find and replace function?

Posted 07 November 2012 - 01:34 AM

View Postlar3ry, on 06 November 2012 - 10:19 AM, said:

View PostJames1992, on 06 November 2012 - 10:36 AM, said:

I've already been able to do that using my own code.

I'm after just selecting the first instance of that word. For example, if you press find next, it selects the first word that matches the specified word in the findcombo box. If you press find next again, then it then selects the second word that matches and so on and so on..

Well, if you do that in your own code, then you already have a solution. I didn't actually spell it out for you, but here it is.

Write this as a subroutine, and call it when you click Find. Where I put the comment ' do your own thing here., save your index values in an array or a list, and after that subroutine returns, go to the first occurrence (using the index already found, if you like).

Keeping track of which index you are currently using, go to the next one when you click Find Next. When you run out of index values, wrap around and do it again.

It's likely only one of several ways to do it, but I think it's a good one.


I am not able to implement this code.

index = form1.RichTextBox.Find(srchterm, index, RichTextBoxFinds.None)

it comes up with the error:

overload resolution failed because no accessible 'find' can called with these arguments.

This is this example on the Internet of how to do it another way.

position = InStr([start,] string1, string2 [, compare])


If I were to know the index of what the above code is doing, am I able to somehow link that in instead, as I feel i understand that code better.

sorry to keep asking questions
Was This Post Helpful? 0
  • +
  • -

#7 James1992   User is offline

  • D.I.C Head

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

Re: How to do a "Find Next" button in a find and replace function?

Posted 07 November 2012 - 04:40 AM

anyone?
Was This Post Helpful? 0
  • +
  • -

#8 lar3ry   User is offline

  • Coding Geezer
  • member icon

Reputation: 314
  • View blog
  • Posts: 1,296
  • Joined: 12-September 12

Re: How to do a "Find Next" button in a find and replace function?

Posted 07 November 2012 - 07:16 AM

View PostJames1992, on 07 November 2012 - 02:34 AM, said:

I am not able to implement this code.

index = form1.RichTextBox.Find(srchterm, index, RichTextBoxFinds.None)

it comes up with the error:
overload resolution failed because no accessible 'find' can called with these arguments.

What is the name of your RichTextBox? Unless you renamed it to "RichTextBox", it will be something like "RichTextBox1" by default.

Forget InStr(). VB.Net methods are much better.

Check that RichTextBox name.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1