Reputation: 53
I'm just starting to work with VB, and I'm a little lost. What I want to do is create a GUI whereby the use browses to a file, chooses it, and then an action (say a certain type of formatting is performed which generates an output file. Ideally, the same sort of browse function would be available for the user to choose where the output file is saved. Some of these actions I have been able to create, but getting them to work together is not happening yet. For instance, I created a Module to do the formatting:
Module Module1
Sub Main()
'Read all lines from MarcEdit-generated file
Dim Lines() As String = IO.File.ReadAllLines("C:\PathToFile\in.txt")
'Loop through each line
For Each line As String In Lines
'First search for any line that starts with '=246'
If Left(line, 4) = "=246" Then
'Check to see if the 9th digit is an 'a'
If line(9) = "a" Then
Dim field246Cleaned As String = line.Substring(0, line.Length - 1)
'Print string and carriage return to out file
My.Computer.FileSystem.WriteAllText("C:\PathToFile\out.txt", field246Cleaned & vbCrLf, True)
Else
My.Computer.FileSystem.WriteAllText("C:\PathToFile\out.txt", line & vbCrLf, True)
End If
Else
Dim nonField246 As String = line
My.Computer.FileSystem.WriteAllText("C:\PathToFile\out.txt", nonField246 & vbCrLf, True)
Continue For
End If
Next
End Sub
End Module
This reads the file, does the requisite formatting, and then creates a file with the changes. But the paths to the in and out files are static, as are the names of the files. It would be nice if I could create a GUI so the user could choose the name of the in file, and choose the location and name of the out file. I can create a form that has a browse button, so the user can choose the file:
Public Class Form1
Public Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles txtFileDirectory.TextChanged
End Sub
Public Sub btnBrowse_Click(sender As Object, e As EventArgs) Handles btnBrowse.Click
Dim myFileDlog As New OpenFileDialog()
'look for files in the c drive
myFileDlog.InitialDirectory = "C:\PathToFile\"
'specifies what type of data files to look for
myFileDlog.Filter = "All Files (*.*)|*.*" & "|Data Files (*.txt)|*.txt"
'specifies which data type is focused on start up
myFileDlog.FilterIndex = 2
'Gets or sets a value indicating whether the dialog box restores the current directory before closing.
myFileDlog.RestoreDirectory = True
'seperates message outputs for files found or not found
If myFileDlog.ShowDialog() = DialogResult.OK Then
If Dir(myFileDlog.FileName) <> "" Then
'MsgBox("File Exists: " & myFileDlog.FileName, MsgBoxStyle.Information)
Else
MsgBox("File Not Found", MsgBoxStyle.Critical)
End If
End If
'Adds the file directory to the text box
'txtFileDirectory.Text = myFileDlog.FileName
Dim testName = myFileDlog.FileName
txtFileDirectory.Text = testName
End Sub
Public Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
Application.Exit()
End Sub
End Class
Is there a way to use the browse function that I coded to find the filename, and then somehow use that filename as a parameter for the module to run? Ideally in the interface there would also be a browse function to set the path and name for the out file, too, which would be be passed to the module. Sorry if this is a vague or stupid question, I'm just starting out in VB!
Upvotes: 1
Views: 247
Reputation: 19330
Just place 2 read only text boxes with a button next to each. On the first button, do OpenFileDialog
to get the file path. On the second button do BrowseForFolder
dialog to set the output directory. And a button to start process.
Label: "Select file" | TextBox: txtFile | Button: btnOpenFile
Label: "Select destination" | TextBox: txtDestination | Button: btnOpenFolder
Button: btnProcessFile
Refactor your method so it takes both, target file and destination folder
Sub ProcessFile(ByVal file as String, ByVal pathOut as String)
'Read all lines from MarcEdit-generated file
Dim Lines() As String = IO.File.ReadAllLines(file)
'Loop through each line
For Each line As String In Lines
'First search for any line that starts with '=246'
If Left(line, 4) = "=246" Then
'Check to see if the 9th digit is an 'a'
If line(9) = "a" Then
Dim field246Cleaned As String = line.Substring(0, line.Length - 1)
'Print string and carriage return to out file
My.Computer.FileSystem.WriteAllText(Path.Combine(pathOut, "out.txt"), field246Cleaned & vbCrLf, True)
Else
My.Computer.FileSystem.WriteAllText(Path.Combine(pathOut, "out.txt"), line & vbCrLf, True)
End If
Else
Dim nonField246 As String = line
My.Computer.FileSystem.WriteAllText(Path.Combine(pathOut, "out.txt"), nonField246 & vbCrLf, True)
Continue For
End If
Next
End Sub
This way, it is clearer for the user what is happening - here is intake, and here is output; correct? - correct - process. There are other things that you could do differently here but since you're learning I didn't refactor them.
Upvotes: 1