在visual basic

时间:2017-05-05 15:52:10

标签: vb.net

我有一个onLoad表单,基本上是一个输入文本框和一个OK / Cancel按钮。单击确定按钮时,我有代码

strFileName = textbox1.text

me.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 个答案:

答案 0 :(得分:0)

如果全局变量没有按照您的要求工作,您可以稍后在onload表单中创建一些属性,可以在form_load中使用它来设置局部变量。