即使应用程序关闭并重新启动,我如何保存用户所做的更改?
How can I save the changes made by the user even when the app closes and restarts?
我了解 .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
因此,在关闭事件之后,我需要在重新打开应用程序时,将字符串保留在各自的文本框中。
您必须将它们保存在某个物理位置(文件或数据库)并在您的应用再次启动时检索它们。
有最简单解决方案,将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、数据库...值可以被加密,等等。
我了解 .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
因此,在关闭事件之后,我需要在重新打开应用程序时,将字符串保留在各自的文本框中。
您必须将它们保存在某个物理位置(文件或数据库)并在您的应用再次启动时检索它们。
有最简单解决方案,将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、数据库...值可以被加密,等等。