在VB中重新初始化变量回到开始状态
问题描述:
我试图询问用户他们是否想通过程序再次玩游戏。如果用户选择Yes
,则所有变量将被设置回开始状态,并且如果他们选择No
,则它们只返回到表单。我遇到的问题是我不希望使用Application.Restart()
,有没有办法在不使用Application.Restart()
的情况下重新初始化程序?在VB中重新初始化变量回到开始状态
代码对于程序:
'This Procedure Disables Play Continuation, Asks User To Play Again, & Initializes Starting Status
Sub DisplayPlayAgain()
'Disable Button That Allows User To Continue Play
cmdGuess.Enabled = False
'Display A Button Asking User To Play Again
Dim answer As DialogResult
MessageBox.Show("Would You Like To Play Again?", " Confirmation of Quit", MessageBoxButtons.YesNo)
If answer = vbYes Then
'Reset Variables back to Starting Status
lstNumberGuess.Items.Clear()
lblTotalCount.Text = String.Empty
lblAnswer.Text = String.Empty
txtUserGuess.Text = String.Empty
lblAnswer.BackColor = Color.FromKnownColor(KnownColor.Control)
shrtCounterTotal = 0
shrtSecret = shrtRandom.Next(1, 100)
'Set Focus Back to textbox
txtUserGuess.Select()
'Allow User to Play Again
Application.Restart()
Else
'Or if No is selected, return to the form
End If
End Sub
答
不能使用Application.Restart()
与您的代码的问题是这样的:
Dim answer As DialogResult
MessageBox.Show("Would You Like To Play Again?", " Confirmation of Quit", MessageBoxButtons.YesNo)
If answer = vbYes Then
你永远分配MessageBox
结果你answer
变量。试试这个:
Dim answer As DialogResult = MessageBox.Show("Would You Like To Play Again?", " Confirmation of Quit", MessageBoxButtons.YesNo)
If answer = vbYes Then
至于重置您的变量。
您有两种选择。首先,您正在尝试执行手动重置它们。简化的东西可以让你使用初始化了一个方法里面你的游戏,始终都调用该方法启动或重新启动游戏:
Sub InitGame()
lstNumberGuess.Items.Clear()
lblTotalCount.Text = String.Empty
lblAnswer.Text = String.Empty
txtUserGuess.Text = String.Empty
lblAnswer.BackColor = Color.FromKnownColor(KnownColor.Control)
shrtCounterTotal = 0
shrtSecret = shrtRandom.Next(1, 100)
'Set Focus Back to textbox
txtUserGuess.Select()
End Sub
所以重新启动:
Sub DisplayPlayAgain()
'Disable Button That Allows User To Continue Play
cmdGuess.Enabled = False
'Display A Button Asking User To Play Again
Dim answer As DialogResult = MessageBox.Show("Would You Like To Play Again?", " Confirmation of Quit", MessageBoxButtons.YesNo)
If answer = vbYes Then
InitGame()
Else
'Or if No is selected, return to the form
End If
End Sub
你的另一种选择是在自己的课堂上定义你的游戏,并且每次你想要开始或重新开始游戏时,只需创建一个新的课程实例。
例如,您可以将您的游戏移动到自己的控件中,并为游戏完成时连接事件。父控制可以决定创建一个新游戏,创建一个新的控件实例并加载它。
事情是这样的:
Public Class GameFinishedArgs
Inherits EventArgs
Public PlayAgain As Boolean
Public Sub New(PlayAgain As Boolean)
Me.PlayAgain = PlayAgain
End Sub
End Class
Public Class Game
Inherits Control
Public Event GameFinished As EventHandler
Public Sub New()
lstNumberGuess.Items.Clear()
lblTotalCount.Text = String.Empty
lblAnswer.Text = String.Empty
txtUserGuess.Text = String.Empty
lblAnswer.BackColor = Color.FromKnownColor(KnownColor.Control)
shrtCounterTotal = 0
shrtSecret = shrtRandom.Next(1, 100)
End Sub
Public Sub Game_Load(Sender As Object, E As EventArgs) Handles MyBase.load
StartGame()
End Sub
Sub DisplayPlayAgain()
'Disable Button That Allows User To Continue Play
cmdGuess.Enabled = False
'Display A Button Asking User To Play Again
Dim answer As DialogResult
answer = MessageBox.Show("Would You Like To Play Again?", " Confirmation of Quit", MessageBoxButtons.YesNo)
RaiseEvent GameFinished(Me, New GameFinishedArgs(answer = vbYes))
End Sub
End Class
Public Class Form1
Inherits System.Windows.Forms.Form
Public Sub Form1_Load(Sender As Object, E As EventArgs) Handles MyBase.Load
LoadGame()
End Sub
Public Sub LoadGame()
Dim GameControl As New Game()
AddHandler GameControl.GameFinished, New EventHandler(AddressOf OnGameFinished)
GameControl.Top = 0
GameControl.Left = 0
GameControl.Anchor = AnchorStyles.Left Or AnchorStyles.Top Or AnchorStyles.Right Or AnchorStyles.Bottom
GameControl.Dock = DockStyle.Fill
Me.Controls.Add(GameControl)
End Sub
Public Sub OnGameFinished(Sender As Object, E As GameFinishedArgs)
Dim GameControl As Game = Sender
RemoveHandler GameControl.GameFinished, New EventHandler(AddressOf OnGameFinished)
If (E.PlayAgain) Then
LoadGame()
End If
End Sub
End Class
+0
谢谢亚历山大 – MB9
未重置变量返回到起始状态就够了吗?究竟是什么问题? –
没有什么魔法可以做。如果您希望将字段设置为特定值,请将其设置为特定值。就是这样,就是这样。唯一的另一种方法是将所有这些字段放入一个类中,以便在需要默认值时创建一个新实例。 – jmcilhinney
如果您重新启动应用程序,重置变量将不起作用,因为重新启动时会再次发生。我假设你需要重新启动你的应用程序启动逻辑中的游戏初始化逻辑。我会推荐的是从一个你可以从你的启动逻辑和再次播放逻辑调用的方法中获得这种方法,然后你不需要调用'Application.Restart()' – Fabulous