
Find and Replace is one of the more useful features you can add to a control that holds text. I created this VB form to do a find first, find next, replace and replace all support.
The form was called as a modal form with .showDialog and a screen shot of just the form can be seen below. The Replace button is not enabled until Find First is clicked and the Replace All button is available without clicking on the find button. Find Next is also disabled until the first string is found. Bottom line: you must click on Find First or Replace All before using the other buttons. The program was written with Microsoft Visual Studio 2008 in Visual Basic.

Processing Logic
The program has been written to take advantage of some richtextbox control (mainForm.rtbBody) members (Find, Replace, SelectedText and SelectionColor) combined with some string functions. The program is reasonably straight forward perhaps with the exception of the richtextbox Find method which needs a search string, a starting point (in chars) to search,the length of the search term and the type of match (wholeword, case, etc. - see VB documentation for richtextbox.find method). The starting point of the find changes to start just after the last find so that find next and replace all can continue to search through the textbox.
You should be able to duplicate the form design and copy the code below into your application to test. Note: I had to truncate some lines with continuation characters to fit inside blog post window.
As always in this blog: Use this program example at your own risk. This code was created for educational purposes only. The author provides programming code "as is" and provides no warranty or guarantee. If you have questions, If you can email the author at gmarrer@gmail.com
Visual Basic.Net 2008 Source Code (with Program Comments)
Public Class findreplace
'class instance variables
Private findText As String ' find text
Private replaceText As String ' replace text
Private selectionStart, selectionLen, lastStart As Integer 'position variables for search
Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles btnClose.Click
'close form
Me.Close()
End Sub
Private Sub btnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles btnFind.Click
'if no text in find textbook
If txtFind.Text <> "' then" Then
'get ready to find the text
findText = txtFind.Text
'find text
selectionStart = mainForm.rtbBody.Find(findText, RichTextBoxFinds.WholeWord)
If selectionStart <>
'text not found
MessageBox.Show(findText & " Text Not Found", "Warning", MessageBoxButtons.OK, _
MessageBoxIcon.Exclamation)
Else
'turn on replace and find next
btnReplace.Enabled = True
btnFindNext.Enabled = True
'set new start point for continued search
lastStart = selectionStart
'start the selected text
mainForm.rtbBody.SelectionStart = selectionStart
'get len of selected text
mainForm.rtbBody.SelectionLength = findText.Length
'mark selected text red in richtextbox
mainForm.rtbBody.SelectionColor = Color.Red
End If
Else
'No text in Find input textbox
MessageBox.Show("No find string entered. Please key in a value to find.", "Error", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
txtFind.Focus()
End If
End Sub
Private Sub btnReplace_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles btnReplace.Click
'text in find textbox
If txtFind.Text <> "" Then '
'text in replace textbox
If txtReplace.Text <> "" Then
'get replace text from textbox
replaceText = txtReplace.Text
'replace selected text found with replacement text
mainForm.rtbBody.SelectedText = replaceText
'mark selected text red in richtextbox
mainForm.rtbBody.SelectionColor = Color.Red
Else
'text in replace textbox missing
MessageBox.Show("No replace string entered. Please key in a value to find.", "Error", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
'put cursor in replace textbox
txtReplace.Focus()
End If
Else
'text in find textbox missing
MessageBox.Show("No find string entered. Please key in a value to find.", "Error", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
'put cursor in find textbox
txtFind.Focus()
End If
End Sub
Private Sub btnFindNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles btnFindNext.Click
'make sure find text is still there - mostly cosmetic
If txtFind.Text <> "' then" Then
'find text
selectionStart = mainForm.rtbBody.Find(findText, lastStart + findText.Length, _
mainForm.rtbBody.TextLength, RichTextBoxFinds.WholeWord)
'no more occurances
If selectionStart <>
MessageBox.Show(findText & " No more occurances Found", "Warning", _
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Else
'select and mark the found text
lastStart = selectionStart
mainForm.rtbBody.SelectionStart = selectionStart
mainForm.rtbBody.SelectionLength = findText.Length
mainForm.rtbBody.SelectionColor = Color.Red
End If
Else
'find input textbox is empty
MessageBox.Show("No find string entered. Please key in a value to find.", "Error", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
'put cusor in find textbox
txtFind.Focus()
End If
End Sub
Private Sub btnReplaceAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles btnReplaceAll.Click
'find and replace textboxes should not be empty
If txtFind.Text <> "" And txtReplace.Text <> "" Then
'store find text
findText = txtFind.Text
'store replace text
replaceText = txtReplace.Text
selectionStart = 0
'find first occarance
selectionStart = mainForm.rtbBody.Find(findText, RichTextBoxFinds.WholeWord)
'set new start point for continued search
lastStart = selectionStart
'Text not found test
If selectionStart <>
MessageBox.Show(findText & " Text Not Found", "Warning", MessageBoxButtons.OK, _
MessageBoxIcon.Exclamation)
Else
'text found replace text and mark with red
lastStart = selectionStart
mainForm.rtbBody.SelectionStart = selectionStart
mainForm.rtbBody.SelectionLength = findText.Length
mainForm.rtbBody.SelectedText = replaceText
mainForm.rtbBody.SelectionColor = Color.Red
End If
'Are there more occurances - loop until none or -1
While selectionStart >= 0
'search for text
selectionStart = mainForm.rtbBody.Find(findText, lastStart + findText.Length, _
mainForm.rtbBody.TextLength, RichTextBoxFinds.WholeWord)
'if no more occrances exit
If selectionStart <>
Exit While
Else
'set new start point for continued search
lastStart = selectionStart
'select text, replace and mark red
mainForm.rtbBody.SelectionStart = selectionStart
mainForm.rtbBody.SelectionLength = findText.Length
mainForm.rtbBody.SelectedText = replaceText
mainForm.rtbBody.SelectionColor = Color.Red
End If
End While
Else
'test must be in find and replace textboxes
MessageBox.Show("Find and Replace Text must both be entered into Textboxes.", _
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
'place cursor in find textbox
txtFind.Focus()
End If
End Sub
End Class
1 comments:
Well done got some nice information, keep going...
Post a Comment