【问题标题】:Saving a text File in visual basic在 Visual Basic 中保存文本文件
【发布时间】: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


【解决方案1】:

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

【讨论】:

  • 我想我可以尝试一组并获得。如果我只是尝试调试并查看它是我的代码还是我的变量,那么诸如 tester.txt 之类的文本文件的默认文件夹是什么
  • 它应该在项目的 bin\Debug 文件夹中。
  • 好吧,我想我已经缩小了范围,它不是变量。它没有在 bin\debug 文件夹中保存任何文件。对下一步做什么有什么建议吗?
  • 我卡得太久了。
猜你喜欢
  • 2018-09-12
  • 1970-01-01
  • 1970-01-01
  • 2012-12-10
  • 2020-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多