我是VS 2012的新手,我想知道如何保存用户输入的内容,以便在重新打开程序时保存。现在我只有2个按钮,每按一次就会增加一个标签,另一个标签将另一个标签分成百分之一。
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim k, d, r As Single
k = Label2.Text + 1
d = Label4.Text
r = (d / k)
Label2.Text = k
Label6.Text = Format(r, "Percent")
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim k, d, r As Single
k = Label2.Text
d = Label4.Text + 1
r = (d / k)
Label4.Text = d
Label6.Text = Format(r, "Percent")
End Sub
答案 0 :(得分:2)
你的情况是你必须将你的输入保存到一些外部文件,并重新打开程序来读取该文件并初始化你的fiels(标签,文本框等)
关于使用vb here
读取写入文件的好教程答案 1 :(得分:1)
使用应用程序设置存储值。
转到项目 - >属性 - >设置标签。 添加“Label2”,“Label4”和“Label6”的条目,将Type保留为String:
现在将代码添加到Form的Load()和FormClosing()事件中,以便在应用程序设置中加载和保存值:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
If My.Settings.Label2 <> "" Then
Label2.Text = My.Settings.Label2
End If
If My.Settings.Label4 <> "" Then
Label4.Text = My.Settings.Label4
End If
If My.Settings.Label6 <> "" Then
Label6.Text = My.Settings.Label6
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim k, d, r As Single
k = Label2.Text + 1
d = Label4.Text
r = (d / k)
Label2.Text = k
Label6.Text = Format(r, "Percent")
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim k, d, r As Single
k = Label2.Text
d = Label4.Text + 1
r = (d / k)
Label4.Text = d
Label6.Text = Format(r, "Percent")
End Sub
Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
My.Settings.Label2 = Label2.Text
My.Settings.Label4 = Label4.Text
My.Settings.Label6 = Label6.Text
My.Settings.Save()
End Sub
End Class