【发布时间】:2017-05-05 15:52:09
【问题描述】:
我有一个 onLoad 表单,它基本上是一个输入文本框和一个确定/取消按钮。单击确定按钮时,我有代码
strFileName = textbox1.text
我.close()
在我的一个模块中,我有公共字符串 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
【问题讨论】:
-
这里的代码太多了。为了提供足够的信息来帮助我们帮助您,您实际上需要包含多少信息?只有 1 个文本框不会有同样的问题吗?如果不需要滚动阅读问题,它看起来会不会更好?请编辑此问题以提供Minimal, Complete, and Verifiable Example。将来请记住这一点。欢迎来到 SO!
-
我会记住这一点,以便进一步询问。谢谢!
标签: vb.net