Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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.

  • *Experts*
Posted

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):

txtEdit.Text = txtEdit.Text.Replace(m_strSearch, "replace text here")

"Being grown up isn't half as fun as growing up

These are the best days of our lives"

-The Ataris, In This Diary

  • *Experts*
Posted
thanks, next question. How do I code a input box for said replacement?

Do you mean how to use that code Bucky showed you with the InputBox? Could you rephrase your question please.

Posted

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...

  • *Experts*
Posted

You can access the controls on the form and their properties. Just

get the values of the Text properties of the textboxes:

 

Dim frm As New myForm()

frm.ShowDialog()
whatever = frm.txtEdit.Text
' etc. etc.

"Being grown up isn't half as fun as growing up

These are the best days of our lives"

-The Ataris, In This Diary

Posted

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?

  • *Experts*
Posted

You cant refer to a textbox on another form just by its name like you are doing:

TxtFind.Text ......

You need to use the Form object you created:

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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...