User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the VB.NET section within the Software Development category of DaniWeb, a massive community of 402,044 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,451 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our VB.NET advertiser: Programming Forums
Views: 729 | Replies: 4
Reply
Join Date: Apr 2008
Posts: 32
Reputation: Pgmer is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 3
Pgmer Pgmer is offline Offline
Light Poster

Find and replace the string

  #1  
Jul 9th, 2008
Hi all.
im writing function to find and replace the string. Im using 2 textbox's one for keyword which is to find and one to to replace. im having having two buttons btnfindnext and btnreplace. if i click on replace button it should select the text first and agin if i click on the same it should replace the string. and if i click on findnext it should select the next keyword in the document and if again btn replace is clicked it should replace the selected text.
Like in Ms word

thanks
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2002
Location: West Virginia
Posts: 375
Reputation: waynespangler is on a distinguished road 
Rep Power: 6
Solved Threads: 37
waynespangler waynespangler is offline Offline
Posting Whiz

Re: Find and replace the string

  #2  
Jul 9th, 2008
Try this:
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TextBox3.Text = "The quick brown fox jumps over the lazy dog."
    End Sub

    Private Sub FindButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FindButton.Click
        Dim idx As Integer = 0
        idx = TextBox3.Text.IndexOf(TextBox1.Text, idx)
        If idx = -1 Then
            MessageBox.Show(TextBox1.Text & " is not in Textbox3")
        Else
            TextBox3.SelectionStart = idx
            TextBox3.SelectionLength = TextBox1.Text.Length
            TextBox3.Focus()
        End If
    End Sub

    Private Sub ReplaceButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ReplaceButton.Click
        If TextBox3.Text.Contains(TextBox1.Text) Then
            TextBox3.Text = TextBox3.Text.Replace(TextBox1.Text, TextBox2.Text)
        Else
            MessageBox.Show(TextBox1.Text & " is not in Textbox3")
        End If
    End Sub

End Class
It has 3 textboxes and 2 buttons.
Wayne

It is hard to understand how a cemetery can raise its burial rates and blame it on the cost of living.
Reply With Quote  
Join Date: Apr 2008
Posts: 32
Reputation: Pgmer is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 3
Pgmer Pgmer is offline Offline
Light Poster

Re: Find and replace the string

  #3  
Jul 10th, 2008
Thnks wayne,
But ur code will search only for fisrt instance of keword and if i click on replace it is replaceing all the instance of key word. if i keep on clicking findnext button it should select and highlight the keyword untill txtend is reached. and when user click on replcae it has to replace the selected text and also it should highlight the next instance of keword in the document if there.
I think its clear to you. please help me out.
Last edited by Pgmer : Jul 10th, 2008 at 5:44 am.
Reply With Quote  
Join Date: Feb 2008
Location: Sivakasi, Tamilnadu, India
Posts: 399
Reputation: selvaganapathy is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 67
selvaganapathy's Avatar
selvaganapathy selvaganapathy is offline Offline
Posting Whiz

Re: Find and replace the string

  #4  
Jul 10th, 2008
Hi,
To search all the instance of keyword, you should explicitly save the last search index.

For this u can use the Variable
  1. Dim iLastIndex as Integer
  2.  
  3. Private Sub FindButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FindButton.Click
  4. Dim idx As Integer = iLastIndex
  5. idx = TextBox3.Text.IndexOf(TextBox1.Text, idx)
  6. If idx = -1 Then
  7. MessageBox.Show(TextBox1.Text & " is not in Textbox3")
  8. Else
  9. TextBox3.SelectionStart = idx
  10. TextBox3.SelectionLength = TextBox1.Text.Length
  11. TextBox3.Focus()
  12. iLastIndex = TextBox1.Text.Length + idx
  13. End If
  14. End Sub

To replace, First u have to select the Text first then replace it.

  1. Dim idx As Integer = iLastIndex
  2. idx = TextBox3.Text.IndexOf(TextBox1.Text, idx)
  3. If idx = -1 Then
  4. MessageBox.Show(TextBox1.Text & " is not in Textbox3")
  5. Else
  6. TextBox3.SelectionStart = idx
  7. TextBox3.SelectionLength = TextBox1.Text.Length
  8. TextBox3.Focus()
  9. iLastIndex = idx + TextBox2.Text.Length
  10. TextBox3.SelectedText = TextBox2.Text
  11. TextBox3.SelectionStart = idx
  12. TextBox3.SelectionLength = TextBox2.Text.Length
  13. End If
The same code to find and Last Three lines for replace.

Also at one stage find will go to end of the file at that time you should reset the iLastIndex variable to 0
Last edited by selvaganapathy : Jul 10th, 2008 at 12:15 pm.
KSG
Reply With Quote  
Join Date: Apr 2008
Posts: 32
Reputation: Pgmer is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 3
Pgmer Pgmer is offline Offline
Light Poster

Re: Find and replace the string

  #5  
Jul 14th, 2008
Thank you.
But your code is replacing the next keyword not selected keyword.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb VB.NET Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the VB.NET Forum

All times are GMT -4. The time now is 11:31 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC