2013-06-02 47 views
0

我對VS 2012非常新奇,我想知道如何保存用戶輸入的內容,以便在他們重新打開程序時保存它。現在我只有兩個按鈕,每按一次它就會將標籤增加1,另一個按另一個將另一個按照百分比分開的標籤。VS Express 2012桌面如何保存用戶的輸入

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

爲我們提供了代碼 –

+0

添加了代碼@NikolaMitev –

回答

2

它你的情況,你有你的輸入保存到一些外部文件,並重新打開程序讀取該文件,並初始化您fiels(標籤,文本框ETS)

很好的教程有關從讀寫用vb here

1

使用應用程序設置 -to文件來存儲值。

轉到項目 - >屬性 - >設置選項卡。 添加項 「Label2的」, 「Label4」 和 「Label6」,離開類型爲字符串:

Project Settings

現在代碼添加到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 
相關問題