0

我对 VS 2012 非常陌生,我想知道如何保存用户输入的内容,以便在他们重新打开程序时它就在那里。现在我只有 2 个按钮,每次按下它们时标签会增加 1,而另一个标签则将一个标签除以百分比。

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
4

2 回答 2

2

在您的情况下,您必须将输入保存到某个外部文件,并在重新打开程序时读取该文件并初始化您的文件(标签、文本框等)

关于在这里使用 vb 读写文件的好教程

于 2013-06-02T01:52:40.220 回答
1

使用应用程序设置来存储值。

转到项目 --> 属性 --> 设置选项卡。为“Label2”、“Label4”和“Label6”添加条目,将类型保留为字符串:

项目设置

现在将代码添加到表单的 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
于 2013-06-02T02:39:44.783 回答