Sunday, August 8, 2010

Find and Replace Code Vb

Imports System.IO

Public Class frmMBBmain


Private Sub btnReplace_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReplace.Click

Dim fdlg As SaveFileDialog = New SaveFileDialog()

If Trim(txtFileTarget1.Text) = "" Then
MsgBox("Please select Text data to replace the '*'.", MsgBoxStyle.Information + MsgBoxStyle.OkOnly)
txtFileTarget1.Focus()
Exit Sub
End If


fdlg.Title = "Save File"
fdlg.InitialDirectory = "C:\"
fdlg.FileName = ""
fdlg.Filter = "Text files (*.txt)|*.txt"
If fdlg.ShowDialog() = DialogResult.OK Then
CombineSourceFile_SCB(fdlg.FileName)
Else
Exit Sub
End If
End Sub
Private Sub CombineSourceFile_SCB(ByVal sDestinationFile As String)


Dim sPrintStmt As String
Dim temp As String
Dim nFileTotalRow As Integer
Dim nRow As Long

Dim dTransTotal As Double = 0
Dim nTransRow As Long = 0

Dim objWriter As StreamWriter
Dim objRead As StreamReader

Dim bReadable As Boolean = False

objWriter = New StreamWriter(sDestinationFile)
'------------------- Looping Data to Text File (Start)-----------------------------

'Header - First Line

sTASK = "Reading File Target - "
bReadable = False

If Trim(txtFileTarget1.Text) <> "" Then
objRead = IO.File.OpenText(txtFileTarget1.Text)
bReadable = True
End If


If bReadable = True Then
While Not objRead.Peek - 1
temp = objRead.ReadLine()
If temp = "" Then
Exit While
End If
nFileTotalRow += 1
Application.DoEvents()

End While
objRead.Close()

objRead = IO.File.OpenText(txtFileTarget1.Text)

While Not objRead.EndOfStream

Application.DoEvents()
temp = objRead.ReadLine()
sPrintStmt = Replace(temp, "*", " ")
objWriter.WriteLine(sPrintStmt)
nRow += 1
End While
objRead.Close()
End If


objWriter.Close()

MsgBox("Replacement Successful!!!", MsgBoxStyle.Information + vbOKOnly)
sTASK = ""
Me.Close()

End Sub

Private Sub cmdClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
txtFileTarget1.Text = ""
End Sub

Private Sub ToolStripBtnTgtScrFile1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripBtnTgtScrFile1.Click
Call LinkPathFile_LinkClicked(txtFileTarget1)
End Sub


Private Sub LinkPathFile_LinkClicked(ByVal ObjectPassIn As Object)
Dim fdlg As OpenFileDialog = New OpenFileDialog()
fdlg.Title = "Open File"
fdlg.InitialDirectory = "C:\"
fdlg.FilterIndex = 2
fdlg.RestoreDirectory = True
If fdlg.ShowDialog() = DialogResult.OK Then
ObjectPassIn.Text = fdlg.FileName
Else
Exit Sub
End If

End Sub

End Class

0 comments:

Post a Comment