JHalstead Posted September 8, 2003 Posted September 8, 2003 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. Quote
*Experts* Bucky Posted September 8, 2003 *Experts* Posted September 8, 2003 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") Quote "Being grown up isn't half as fun as growing up These are the best days of our lives" -The Ataris, In This Diary
JHalstead Posted September 8, 2003 Author Posted September 8, 2003 thanks, next question. How do I code a input box for said replacement? Quote
*Experts* mutant Posted September 8, 2003 *Experts* Posted September 8, 2003 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. Quote
JHalstead Posted September 8, 2003 Author Posted September 8, 2003 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... Quote
*Experts* mutant Posted September 8, 2003 *Experts* Posted September 8, 2003 Oh, you cant change how InputBoxes work. You would have to create your own form that asks for that input. Quote
JHalstead Posted September 8, 2003 Author Posted September 8, 2003 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?? Quote
*Experts* Bucky Posted September 8, 2003 *Experts* Posted September 8, 2003 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. Quote "Being grown up isn't half as fun as growing up These are the best days of our lives" -The Ataris, In This Diary
JHalstead Posted September 9, 2003 Author Posted September 9, 2003 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? Quote
*Experts* mutant Posted September 10, 2003 *Experts* Posted September 10, 2003 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. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.