VB projects - Search/replace text
Description: Writing the program that allow searching/replacing text in opened file
Minimum requirements: vb5
Download: source code
Screenshot:

Project: Standard EXE
Controls: txtFile (textbox), txtFind (textbox), txtReplace (textbox), cmdFile (button), cmdFindFirst (button), cmdFindNext (button), cmdReplace (button), cmdReplaceAll (button), dlg (common dialog control)
Code:
Private Sub cmdFile_Click()
Dim strTemp As String
txtFile = ""
dlg.FileName = "*.txt"
dlg.ShowOpen
If Dir(dlg.FileName) <> "" Then
Open dlg.FileName For Input As 1
While Not EOF(1)
Line Input #1, strTemp
txtFile = txtFile &
strTemp & vbCrLf
Wend
Close #1
Else
MsgBox "File not found"
End If
End Sub
Private Sub cmdFindFirst_Click()
If txtFind <> "" Then
If InStr(txtFile, txtFind) <> 0 Then
txtFile.SelStart =
InStr(txtFile, txtFind) - 1
txtFile.SelLength =
Len(txtFind)
Else
MsgBox "Not
found"
End If
End If
End Sub
Private Sub cmdFindNext_Click()
If txtFind <> "" Then
txtFile.SelStart = txtFile.SelStart + 2
If InStr(txtFile.SelStart, txtFile, txtFind)
<> 0 Then
txtFile.SelStart =
InStr(txtFile.SelStart, txtFile, txtFind) - 1
txtFile.SelLength =
Len(txtFind)
Else
MsgBox "Not
found"
End If
End If
End Sub
Private Sub cmdReplace_Click()
txtFile.SelText = txtReplace
cmdFindNext_Click
End Sub
Private Sub cmdReplaceAll_Click()
If txtFind <> "" And txtFind <> txtReplace Then
While InStr(txtFile, txtFind) <> 0
txtFile = Left(txtFile,
InStr(txtFile, txtFind) - 1) & txtReplace & Mid(txtFile, InStr(txtFile, txtFind) +
Len(txtFind))
Wend
End If
End Sub
|