vb.net 在 Visual Basic 中创建一个简单的游戏分数管理器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16496378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating a simple Game score manager in Visual Basic?
提问by Cellular
I want to make a scoreboard in visual basic that allows the user to type in a certain number and it adds to the score every time.
我想在visual basic中制作一个记分牌,允许用户输入一定的数字,并且每次都会增加分数。
This is the kind of layout I am using
There are two teams, when a user enters the number in the text box (white one) it will comeup on the grey textbox above, and it will add every time the user types in a number.
有两个团队,当用户在文本框(白色)中输入数字时,它会出现在上面的灰色文本框中,并且每次用户输入数字时都会添加。
Also, how can I display a warning message when a user enters a invalid data? EG - a letter.
另外,当用户输入无效数据时如何显示警告消息?EG - 一封信。
回答by qwr
You can use simple XMlserialize for storing score values outside of application .Every time when you open application you can read you object and every time when you exit you can store your objects .Read more : http://support.microsoft.com/kb/316730Your simple serializable class will be such :
您可以使用简单的 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
回答by Idle_Mind
This will work for positive integers only:
这仅适用于正整数:
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

