Click to See Complete Forum and Search --> : searching for strings
January 21st, 2000, 06:00 PM
if I had a rich text box (richtextbox1), how would I make a FIND/REPLACE thing like in Word or Notepad?
I tried instr but all it returns is the position of the string. I want to select it as well, and when the user clicks next, continue on...
And how can I replace it?
JimmyT
January 21st, 2000, 09:12 PM
this is from the help file for selStart, selText, etc...
private Sub Form_Load ()
Text1.Text = "Two of the peak human experiences"
Text1.Text = Text1.Text & " are good food and classical music."
End Sub
private Sub Form_Click ()
Dim Search, Where ' Declare variables.
' get search string from user.
Search = InputBox("Enter text to be found:")
Where = InStr(Text1.Text, Search) ' Find string in text.
If Where then ' If found,
Text1.SetFocus
Text1.SelStart = Where - 1 ' set selection start and
Text1.SelLength = len(Search) ' set selection length.
rem This is not in help file, but use it to
rem set the new text into your box
text1.seltext = ReplacementText
else
MsgBox "string not found." ' Notify user.
End If
End Sub
Similar code will work for your rich text box.
Note the use of the 'Where' variable. It stores the location that is returned from the Instr call.
Good Luck...
Chris Eastwood
January 22nd, 2000, 05:24 AM
Here's an even easier way that uses the built-in find method of the RTF control :
Place a RichTextBox and Button on your form :
'
private Sub Command1_Click()
Dim lFoundAt as Long
'
lFoundAt = RichTextBox1.Find("a test", 1, len(RichTextBox1.Text))
'
If lFoundAt >= 0 then
RichTextBox1.SelText = "a great! Test"
End If
'
End Sub
'
private Sub Form_Load()
RichTextBox1.Text = "This is a test"
RichTextBox1.HideSelection = false
'
End Sub
There are lots of different options you can use with the '.find' option - checkout the help that comes with VB or look in the MSDN.
Chris Eastwood
CodeGuru - the website for developers
http://codeguru.developer.com/vb
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.