Find Replace

JHalstead

Newcomer
Joined
Sep 8, 2003
Messages
5
Here is a very simple request, or so I thought.

Ho do I code a Find and Replace function into my VB.Net program?

I have a very basic find function

Public Sub Find()
Dim lngPos As Long

m_strSearch = InputBox("Enter the text to find.", "Find Text")
If m_strSearch = "" Then Exit Sub
lngPos = InStr(1, txtEdit.Text, m_strSearch, vbTextCompare)
If lngPos > 0 Then
txtEdit.SelectionStart = lngPos - 1
txtEdit.SelectionLength = Len(m_strSearch)
Else
m_strSearch = ""
MsgBox("Search text was not found.", vbExclamation, "No Found Text")
End If
End Sub

But I have no clue as to a replace function.

Please help.
 
How about the String class's Replace function? Unlike of all the
finding code that you have above, a Find-Replace situation just
requires one call to the Replace method (in its simplest form; you
may want to write more code for wildcard matching and case
insensitivity):
Visual Basic:
txtEdit.Text = txtEdit.Text.Replace(m_strSearch, "replace text here")
 
I got it working great execpt one thing. There is a message box that askes for the text you are looking for that after you press ok there is another message box that askes for the text that you want to replace with. I would like to combine these to seperate message boxes into one popup.

Hope this is more understandable...
 
Ok, now were getting somewhere. I know how to create custom forms but how do I get it to link to txtEdit in the origional form? Does it work that out by its' self??
 
You can access the controls on the form and their properties. Just
get the values of the Text properties of the textboxes:

Visual Basic:
Dim frm As New myForm()

frm.ShowDialog()
whatever = frm.txtEdit.Text
' etc. etc.
 
Private Sub Replace()
Dim frm As New TextEditor()

If txtFind.Text = "" Then Exit Sub
If txtReplace.Text = "" Then Exit Sub

frm.txtEdit.Text.Replace("txtFind.Text", "txtReplace.Text")
End Sub


This is what I have right now and it is not working. I am still confused on how to get it to link to my texteditor.txtedit textbox.

Is this code wrong?
 
You cant refer to a textbox on another form just by its name like you are doing:
Visual Basic:
TxtFind.Text ......
You need to use the Form object you created:
Visual Basic:
frm.TxtFind.Text....
Also, dont use quotes around the object name like you did with the Replace method, same thing applies as what I said above.
Also I dont see you showing the form in any way. Try what Bucky said above, frm.ShowDialog(), before checking any values.
 
Back
Top