如何将单个按钮用于多个事件Visual Basic

时间:2017-05-06 17:42:22

标签: vb.net

对于投资组合项目,我试图使用Windows窗体创建一个游戏,我遇到了这个我似乎无法弄清楚的问题,

我有一个游戏画面,要求用户输入问题的答案,然后按下一个按钮将答案=设置为变量或执行某种操作,然后让该按钮也输出下一个问题到列表框。

我可以合并一下这会让下一个列表框输出等待按钮点击吗?如果是这样我将如何做到这一点,我似乎无法在任何地方找到任何关于它的东西。

Public Class frmGame
    Public Sub frmGame_Load(sender As Object, e As EventArgs) Handles MyBase.Load ' main game window, dim's variables
        lstOutput.Items.Add("Please enter your name")
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnNext.Click 'Next button, advances the game
        lstOutput.ClearSelected()
        Dim name As String = txtInput.Text

        lstOutput.Items.Add("Are you a boy or a girl, enter the number of your choice")
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles btnNext.Click 'Next button, advances the game
        lstOutput.ClearSelected()
        Dim name As String = txtInput.Text

        lstOutput.Items.Add("e")
        Dim gender As String = txtInput.Text
    End Sub

    Private Sub btnReset_Click(sender As Object, e As EventArgs) Handles btnReset.Click 'clears textbox for user input

    End Sub

    Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub
End Class

1 个答案:

答案 0 :(得分:0)

  

我希望程序能够提出问题,拥有用户类型   在文本框中回答该问题,然后点击" next"按键   并出现一个新问题

创建代表问答数据的类

Public Class Question    
    Public ReadOnly Property Text As String
    Public Property Answer As String

    Public Sub New(text As String)
        Me.Text = text
    End Sub
End Class

然后初始化问题集合并在"下一步"按钮单击移动整个问题集。

Public Class frmGame
    Private _questions As List(Of Question)
    Private _currentQuestionIndex As Integer = -1

    Public Sub frmGame_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        _questions = Return New List(Of Question) From
        {
            new Question("Please enter your name"),
            new Question("Question 1"),
            new Question("Question 2"),
            new Question("Last question")
        }

        MoveToNextQuestion()
    End Sub

    Private Sub ButtonNext_Click(sender As Object, e As EventArgs) Handles btnNext.Click
        Dim currentQuestion = _questions[_currentQuestionIndex]
        currentQuestion.Answer = txtInput.Text

        If IsCurrentQuestionLast() Then
            ProcessAnswers()
        Else
            MoveToNextQuestion()
        End If
    End Sub

    Private Sub MoveToNextQuestion()
        _currentQuestionIndex += 1

        txtInput.Text = Stirng.Empty
        txtQuestionText.Text = _questions[_currentQuestionIndex]
    End Sub

    Private Function IsCurrentQuestionLast()
        Return _currentQuestionIndex = _questions.Count - 1
    End Function

    Private Sub ProcessAnswers()
        For Each question In _questions
            ' Do something with question.Answer
        Next
    End Sub
End Class