在Visual Basic中创建一个简单的游戏得分管理器?

时间:2013-05-11 11:00:58

标签: vb.net

我想在visual basic中制作一个记分牌,允许用户输入一定的数字,并且每次都会增加得分。

This is the kind of layout I am using

有两个团队,当用户在文本框中输入数字(白色)时,它将出现在上面的灰色文本框中,并且每次用户输入数字时都会添加。

另外,当用户输入无效数据时,如何显示警告消息? EG - 一封信。

2 个答案:

答案 0 :(得分:1)

您可以使用简单的XMlserialize在应用程序之外存储得分值。每次打开应用程序时,您都可以读取对象,每次退出时都可以存储对象。阅读更多内容: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

答案 1 :(得分: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