2013-05-11 28 views

回答

1

您可以使用簡單的XMLSERIALIZE用於存儲應用程序。每次外面的分值,當你打開的應用程序,你可以閱讀你的對象,每次當你退出,你可以存儲你的對象.Read更多:http://support.microsoft.com/kb/316730 你簡單的序列化類會是這樣的:

<Serializable()> 
Class UsersList 

     Public Property members As List(Of User) 
     Sub New() 
      members = New List(Of User) 
     End Sub 

     Public Sub add(user As User) 
      If IsNothing(members) = False Then 
       members.Add(user) 
      End If 

     End Sub 
    End Class 
    Class User 
     Public scores As List(Of Single) 
     Public Property name As String 
     Sub New() 
      scores = New List(Of Single) 
     End Sub 

     Public Sub add(score As Single) 
      If IsNothing(scores) = False Then 
       scores.Add(score) 
      End If 

     End Sub 

    End Class 

And For user input you can do two ways : 

     'Displaying warning when it is not valid float number 
    'works for floating numbers too 
      Private Sub TextBox_TextChanged(sender As System.Object, e As System.EventArgs) Handles YouTextbox1.TextChanged,YourTextbox2.TextChanged 
       Dim cheked As TextBox = CType(sender, TextBox) 
       If IsNothing(cheked) = False Then 
        Dim f As Single 
        If Single.TryParse(cheked.Text, f) = False Then 
         MessageBox.Show("Warning .Please enter valid number") 

        End If 

       End If 
      End Sub 
     'not allow user enter to type wrong keys 
     Private Sub TextBox1_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox.KeyPress 
       'Disallow user type anything besides numbers 
       If e.KeyChar < CChar("0") Or e.KeyChar > CChar("9") Then 
        e.Handled = True 
       End If 
      End Sub 
+0

您好,感謝您的評論。 我不是很熟悉你提到的XMSerialize,我只是想要一個應用程序,它將添加到文本框中的插入數字添加到另一個文本框中。 所以textbox1的默認值爲「0」,用戶在文本框2(整數)中輸入一個數字,然後按下按鈕,文本框2中的值被添加到文本框1. – Cellular 2013-05-11 12:08:56

+0

那麼對於您的情況就是這樣:If Single .TryParse(TextBox2.Text,f)= true然後sum + = f TextBox1.Text = sum.ToString()TextBox1.Text =「0」...你有這樣的想法嗎?在我以前的回答中,你可以找到簡單的遊戲分數管理器和輸入數字驗證警告。 – qwr 2013-05-11 13:55:07

+0

是的,明白了。謝謝:D – Cellular 2013-05-11 14:44:02

0

這將只正整數工作:

Public Class Form1 

    Private Score1 As Integer = 0 
    Private score2 As Integer = 0 

    Public Const GWL_STYLE As Integer = (-16) 
    Public Const ES_NUMBER As Integer = &H2000 

    Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" _ 
     (ByVal handle As IntPtr, ByVal nIndex As Integer) As Integer 

    Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _ 
     (ByVal handle As IntPtr, ByVal nIndex As Integer, ByVal dwNewLong As Integer) As Integer 

    Public Sub SetNumbersOnlyTextBox(ByVal TB As TextBox) 
     SetWindowLong(TB.Handle, GWL_STYLE, GetWindowLong(TB.Handle, GWL_STYLE) Or ES_NUMBER) 
    End Sub 

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load 
     SetNumbersOnlyTextBox(txtScore1) 
     SetNumbersOnlyTextBox(txtScore1) 
     DisplayScores() 
    End Sub 

    Private Sub btnAddScore1_Click(sender As System.Object, e As System.EventArgs) Handles btnAddScore1.Click 
     If txtScore1.Text.Trim.Length > 0 Then 
      Score1 = Score1 + CInt(txtScore1.Text) 
      DisplayScores() 
      txtScore1.Clear() 
     End If 
    End Sub 

    Private Sub btnAddScore2_Click(sender As System.Object, e As System.EventArgs) Handles btnAddScore2.Click 
     If txtScore2.Text.Trim.Length > 0 Then 
      score2 = score2 + CInt(txtScore2.Text) 
      DisplayScores() 
      txtScore2.Clear() 
     End If 
    End Sub 

    Private Sub DisplayScores() 
     lblScore1.Text = Score1 
     lblScore2.Text = score2 
    End Sub 

End Class