Vb.net 在visual basic中保存文本文件
我有一个onLoad表单,基本上是一个输入文本框和一个OK/Cancel按钮。单击OK按钮时,我有代码 strFileName=textbox1.text 我 在我的一个模块中,我有一个公共字符串strFileName 我的问题是,如果我不在文本框中放一个字符串,让它进入我的主窗体。当我单击“保存”时,我的“保存”对话框将正确保存我的代码。但是,如果我在onLoad textbox.text中输入一个扩展名为.txt的字符串,则不会创建文本文件,或者至少我找不到它的创建位置。如何将信息输入到文本框中Vb.net 在visual basic中保存文本文件,vb.net,Vb.net,我有一个onLoad表单,基本上是一个输入文本框和一个OK/Cancel按钮。单击OK按钮时,我有代码 strFileName=textbox1.text 我 在我的一个模块中,我有一个公共字符串strFileName 我的问题是,如果我不在文本框中放一个字符串,让它进入我的主窗体。当我单击“保存”时,我的“保存”对话框将正确保存我的代码。但是,如果我在onLoad textbox.text中输入一个扩展名为.txt的字符串,则不会创建文本文件,或者至少我找不到它的创建位置。如何将信息输入到文本
Imports System.IO
Public Class Form1
Private employee As EmployeeInfo
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
txtFirstName.Clear()
txtMiddleName.Clear()
txtLastName.Clear()
txtEmployeeNumber.Clear()
cbDepartment.SelectedIndex = -1
cbDepartment.Text = String.Empty
txtTelephone.Clear()
txtExtension.Clear()
txtEmailAddress.Clear()
txtFirstName.Focus()
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub btnSaveRecord_Click(sender As Object, e As EventArgs) Handles btnSaveRecord.Click
employee.firstName = txtFirstName.Text
employee.middleName = txtMiddleName.Text
employee.lastName = txtLastName.Text
If Not Decimal.TryParse(txtEmployeeNumber.Text, employee.employeeNumber) Then
MessageBox.Show("Please enter a valid number.")
Exit Sub
End If
employee.department = cbDepartment.Text
employee.telephone = txtTelephone.Text Then
employee.extension = txtExtension.Text
employee.emailAddress = txtEmailAddress.Text
If strFileName = String.Empty Then
If sfdSaveFile.ShowDialog = DialogResult.OK Then
strFileName = sfdSaveFile.FileName
SaveDocument()
Else
SaveDocument()
End If
End If
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim onLoad As New onLoad
onLoad.ShowDialog()
End Sub
Sub SaveDocument()
Dim outputFile As StreamWriter
If Not File.Exists(strFileName) Then
Try
outputFile = File.CreateText(strFileName)
outputFile.WriteLine(employee.firstName)
outputFile.WriteLine(employee.middleName)
outputFile.WriteLine(employee.lastName)
outputFile.WriteLine(employee.employeeNumber)
outputFile.WriteLine(employee.department)
outputFile.WriteLine(employee.telephone)
outputFile.WriteLine(employee.extension)
outputFile.WriteLine(employee.emailAddress)
outputFile.Close()
Catch ex As Exception
MessageBox.Show("Error Creating File")
End Try
Else
Try
outputFile = File.AppendText(strFileName)
outputFile.WriteLine(employee.firstName)
outputFile.WriteLine(employee.middleName)
outputFile.WriteLine(employee.lastName)
outputFile.WriteLine(employee.employeeNumber)
outputFile.WriteLine(employee.department)
outputFile.WriteLine(employee.telephone)
outputFile.WriteLine(employee.extension)
outputFile.WriteLine(employee.emailAddress)
outputFile.Close()
Catch ex As Exception
MessageBox.Show("Error Opening File")
End Try
End If
End Sub
End Class
如果全局变量不能按照您的要求工作,您可以稍后在onload表单中创建一些属性,这些属性可以在form\u load中用于设置局部变量。我想我可以尝试设置并获取。如果我只是尝试调试并查看它是我的代码还是我的变量,文本文件(如tester.txt)将转到的默认文件夹是什么?它应该位于project的bin\debug文件夹中。我想我已经缩小了它的范围,而不是变量。它没有在bin\debug文件夹中保存任何文件。对下一步做什么有什么建议吗?我被困太久了。这里的代码太多了。为了提供足够的信息帮助我们,您实际需要包括多少?如果只有一个文本框,你不会有同样的问题吗?如果这个问题不需要滚动阅读,看起来不是更好吗?请编辑此问题以提供答案。今后请记住这一点。欢迎来到SO!我会记住这一点,以便进一步查询。非常感谢。