即使应用关闭并重新启动,如何保存用户所做的更改?

时间:2017-01-15 08:59:18

标签: vb.net formclosing

我了解.FormClosing事件,但是我找不到让用户做出更改的方法,即使应用程序完全关闭,也会重新打开。

我试图让一些字符串值保留在用户输入的文本框中。例如:

Public Class PersonalInfo

 Dim Name as String = ""
 Dim LastName as String = ""

    Sub NameAndLastName()
        Name = TextBox1.Text
        LastName = TextBox2.Text
    End Sub


    Private Sub Button1_Click(...) Handles Button1.Click
        NameAndLastName()
        Me.Close()
    End Sub

End Class

所以在这个结束事件之后,当我重新打开应用程序时,我需要将字符串保留在各自的文本框中。

1 个答案:

答案 0 :(得分:1)

您必须将它们保存在物理位置(文件或数据库)并在应用程序重新启动时检索它们。

最简单的 解决方案将TextBox值保存到txt文件中,并在启动应用程序时检索它们。

Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        'open new file called saveddata.txt and store each textbox value in new line
        Dim fl As New System.IO.StreamWriter(Application.StartupPath & "\saveddata.txt", False)
        fl.WriteLine(TextBox1.Text)
        fl.WriteLine(TextBox2.Text)
        fl.Close()
    End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'check if saveddata.txt file exist and, if exist, take values from it and store to textboxes
        If System.IO.File.Exists(Application.StartupPath & "\saveddata.txt") = True Then
            Dim fl As New System.IO.StreamReader(Application.StartupPath & "\saveddata.txt")
            TextBox1.Text = fl.ReadLine
            TextBox2.Text = fl.ReadLine
            fl.Close()
        End If
    End Sub

这是最简单的解决方案 。您可以将这些值存储到xml中,数据库...值可以加密,等等。