Logo
English Russian German French Spanish Italian
contact usprivacy
   Support Forums
chart
• Adjust flexgrid cell
• Animation
• Centering form text
• Coffee machine
• Creating fileshares
• Creating shortcuts
• Custom buttons
• Directory browser
• Disable mouse events
• File search by ext
• File transfer
• File watcher
• Formatting flexgrid
• Get Content Type
• Get HTML source
• Get modem port
• HTTP proxy
• ipconfig
• Large file split/merge
• MAPI
• MCI Sound Player
• Menu with images
• MP3 normalizer
• Net Send
• Netstat 2000
• No duplicate entries
• Outlook Address Book
• Set font color
• Shapes
• SOAP test
• Text-to-image
• Text file viewer
• Text find/replace
• UPS component
• View NT groups
• Word template
• Writing DNS control
    • Using DNS control
• Writing SMTP control
    • Sending email
    • Mailing list
• Writing WhoIs control
    • Using WhoIs control
• View HTML source
VB projects - Search/replace text

Description: Writing the program that allow searching/replacing text in opened file
Minimum requirements: vb5
Download: source code
Screenshot:
text find/replace (3017 bytes)
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

Copyright © 1996-2008 OstroSoft. All rights reserved. info@ostrosoft.com