将变量重新初始化为VB中的启动状态

时间:2017-07-15 14:39:58

标签: vb.net

我试图询问用户是否希望通过程序再次玩游戏。如果用户选择Yes,则所有变量都会重新设置为开始状态,如果他们选择No,则只返回表单。我遇到的问题是我不想使用Application.Restart(),有没有办法在不使用Application.Restart()的情况下初始化程序重新启动?

程序守则:

 '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

1 个答案:

答案 0 :(得分:1)

您无法使用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