将VB数据条目保存到文本文件

时间:2013-12-07 14:39:16

标签: vb.net visual-studio-2010 visual-studio vba

所以,我写了这段代码,它接收用户输入的信息。

    Public Class Patient
    Private Sub Patient_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    Label1.Text = "First Name:"
    Label2.Text = "Last Name:"
    Label3.Text = "Address: "
    Label4.Text = "City:"
    Label5.Text = "Province: "
    Label6.Text = "Postal Code: "
    Label7.Text = "Health Card Number: "

    If (CollectionIndexValue <> -1) Then
        Dim existingPatient As New PatientObject4

        existingPatient = PatientCollection(CollectionIndexValue)

        TextBox1.Text = existingPatient.Firstname
        TextBox2.Text = existingPatient.LastName
        TextBox3.Text = existingPatient.Address
        TextBox4.Text = existingPatient.City
        TextBox5.Text = existingPatient.Province
        TextBox6.Text = existingPatient.PostalCode
        TextBox7.Text = existingPatient.HealthCardNumber
        Button1.Text = "Save"

    Else
        Button1.Text = "Add"
    End If
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    If (TextBox1.Text = "" Or TextBox2.Text = "" Or TextBox3.Text = "" Or TextBox4.Text = "" Or
       TextBox5.Text = "" Or TextBox6.Text = "" And TextBox7.Text = "") Then
        Label8.Text = "All fields must be populated"
    Else
        If (Button1.Text.ToUpper() = "ADD") Then
            Dim PatientObject As New PatientObject4

            PatientObject.Firstname = TextBox1.Text
            PatientObject.LastName = TextBox2.Text
            PatientObject.Address = TextBox2.Text
            PatientObject.City = TextBox4.Text
            PatientObject.Province = TextBox5.Text
            PatientObject.HealthCardNumber = TextBox7.Text

            PatientCollection.Add(PatientObject)

            Label8.Text = "Patient Added"
            TextBox1.Text = ""
            TextBox2.Text = ""
            TextBox3.Text = ""
            TextBox4.Text = ""
            TextBox5.Text = ""
            TextBox6.Text = ""
            TextBox7.Text = ""

        Else
            Dim PatientSave As New PatientObject4
            PatientSave.Firstname = TextBox1.Text
            PatientSave.LastName = TextBox2.Text
            PatientSave.Address = TextBox3.Text
            PatientSave.City = TextBox4.Text
            PatientSave.Province = TextBox5.Text
            PatientSave.PostalCode = TextBox6.Text
            PatientSave.LastName = TextBox7.Text

            PatientCollection(CollectionIndexValue) = PatientSave

            Label8.Text = "Patient Saved"
        End If

    End If

End Sub
End Class

如果我希望在添加新患者时将此信息保存到文本文件(创建新的文本文件)。怎么会这样做?我试图谷歌它,但当你不知道你在寻找什么时,它有点难。

非常感谢任何帮助/想法。

感谢。

P.S。这只是更大代码的片段。谢谢。

1 个答案:

答案 0 :(得分:1)

对于任何与文件相关的内容,请查看System.IO命名空间。 IE浏览器; File,StringWriter,TextWriter。文件here有一个快速而简单的示例。