-2

我想在 Visual Basic 中制作一个记分牌,允许用户输入某个数字,并且每次都会增加分数。

这是我正在使用的布局

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

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

4

2 回答 2

1
于 2013-05-11T11:33:30.707 回答
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
于 2013-05-11T12:56:58.207 回答