•
•
•
•
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
![]() |
•
•
Join Date: Apr 2008
Posts: 32
Reputation:
Rep Power: 1
Solved Threads: 3
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
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
•
•
Join Date: Dec 2002
Location: West Virginia
Posts: 375
Reputation:
Rep Power: 6
Solved Threads: 37
Try this:
It has 3 textboxes and 2 buttons.
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 Wayne
It is hard to understand how a cemetery can raise its burial rates and blame it on the cost of living.
It is hard to understand how a cemetery can raise its burial rates and blame it on the cost of living.
•
•
Join Date: Apr 2008
Posts: 32
Reputation:
Rep Power: 1
Solved Threads: 3
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.
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.
•
•
Join Date: Feb 2008
Location: Sivakasi, Tamilnadu, India
Posts: 399
Reputation:
Rep Power: 1
Solved Threads: 67
Hi,
To search all the instance of keyword, you should explicitly save the last search index.
For this u can use the Variable
To replace, First u have to select the Text first then replace it.
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
To search all the instance of keyword, you should explicitly save the last search index.
For this u can use the Variable
VB.NET Syntax (Toggle Plain Text)
Dim iLastIndex as Integer Private Sub FindButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FindButton.Click Dim idx As Integer = iLastIndex 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() iLastIndex = TextBox1.Text.Length + idx End If End Sub
To replace, First u have to select the Text first then replace it.
Vb.NET Syntax (Toggle Plain Text)
Dim idx As Integer = iLastIndex 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() iLastIndex = idx + TextBox2.Text.Length TextBox3.SelectedText = TextBox2.Text TextBox3.SelectionStart = idx TextBox3.SelectionLength = TextBox2.Text.Length End If
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
![]() |
•
•
•
•
•
•
•
•
DaniWeb VB.NET Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Better Solution? String Builder? Censor List Function (ASP.NET)
- Replacing items in a string using a loop? (Python)
- string replace (C++)
- Find And Replace DailougBox (VB.NET)
- Reading a string (C)
- how to change a word in a string? (C)
- Conver int Array into a String (Java)
Other Threads in the VB.NET Forum
- Previous Thread: Update syntax error
- Next Thread: Checkbox problem in VB.NET


Linear Mode