我刚刚开始使用VB,我有点失落。我想要做的是创建一个GUI,用户浏览文件,选择它,然后执行一个动作(比如执行某种类型的格式化生成输出文件。理想情况下,可以使用相同类型的浏览功能用户可以选择保存输出文件的位置。我已经能够创建一些这些操作但是让它们一起工作还没有发生。例如,我创建了一个模块来进行格式化:
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
这将读取文件,执行必需的格式设置,然后创建包含更改的文件。但是输入和输出文件的路径是静态的,文件的名称也是如此。如果我可以创建一个GUI,那么用户可以选择in文件的名称,并选择out文件的位置和名称。我可以创建一个具有浏览按钮的表单,以便用户可以选择文件:
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
有没有办法使用我编码的浏览函数来查找文件名,然后以某种方式使用该文件名作为模块运行的参数?理想情况下,在界面中还有一个浏览功能,用于设置输出文件的路径和名称,这将被传递给模块。对不起,如果这是一个模糊或愚蠢的问题,我刚开始用VB!
答案 0 :(得分:1)
只需放置2个只读文本框,每个文本框旁边都有一个按钮。在第一个按钮上,执行OpenFileDialog
以获取文件路径。在第二个按钮上执行BrowseForFolder
对话框以设置输出目录。还有一个开始处理的按钮。
Label: "Select file" | TextBox: txtFile | Button: btnOpenFile
Label: "Select destination" | TextBox: txtDestination | Button: btnOpenFolder
Button: btnProcessFile
重构您的方法,使其同时包含目标文件和目标文件夹
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
这样,对于用户来说更清楚的是发生了什么 - 这里是摄入量,这里是输出;正确? - 正确 - 过程。 还有其他一些你可以在这里做的不同,但是因为你正在学习我没有重构它们。