Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VB.Net ComboBox.SelectedIndex始终恢复为1_Vb.net_Visual Studio 2010_Combobox - Fatal编程技术网

VB.Net ComboBox.SelectedIndex始终恢复为1

VB.Net ComboBox.SelectedIndex始终恢复为1,vb.net,visual-studio-2010,combobox,Vb.net,Visual Studio 2010,Combobox,我正在写一个石头剪刀游戏,必须使用组合框来获得用户的选择。问题是,无论我选择什么,所选索引总是恢复为1,因此用户总是选择rock。我怎样才能解决这个问题 Imports System.Random Public Class Form1 Dim played As Integer = 0 Dim won As Integer = 0 Dim lost As Integer = 0 Dim draw As Integer = 0 Dim percent As

我正在写一个石头剪刀游戏,必须使用组合框来获得用户的选择。问题是,无论我选择什么,所选索引总是恢复为1,因此用户总是选择rock。我怎样才能解决这个问题

Imports System.Random

Public Class Form1
    Dim played As Integer = 0
    Dim won As Integer = 0
    Dim lost As Integer = 0
    Dim draw As Integer = 0
    Dim percent As Single = 0.0


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Label3.Visible = False
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim a As New Random()
        Dim b As Integer = a.Next(1, 4)
        Dim selection As Integer
        ComboBox1.SelectedIndex = selection

        If b = 1 Then
            Label3.Text = "Rock"
            Label3.Visible = True

            Select Case selection
                Case selection = 1
                    MsgBox("Draw.")
                    draw += 1
                Case selection = 2
                    MsgBox("Paper Covers Rock. You win!")
                    won += 1
                Case selection = 3
                    MsgBox("Rock Crushes Scissors. You lose.")
                    lost += 1
            End Select

        ElseIf b = 2 Then
            Label3.Text = "Paper"
            Label3.Visible = True

            Select Case selection
                Case selection = 1
                    MsgBox("Paper Covers Rock. You lose.")
                    lost += 1
                Case selection = 2
                    MsgBox("Draw.")
                    draw += 1
                Case selection = 3
                    MsgBox("Scissors Cuts Paper. You win!")
                    won += 1
            End Select

        ElseIf b = 3 Then
            Label3.Text = "Scissors"
            Label3.Visible = True

            Select Case selection
                Case selection = 1
                    MsgBox("Rock Crushes Scissors. You win!")
                    won += 1
                Case selection = 2
                    MsgBox("Scissors Cuts Paper. You lose.")
                    lost += 1
                Case selection = 3
                    MsgBox("Draw.")
                    draw += 1
            End Select

        End If

        played += 1
        percent = (won / played) * 100

        PlayedText.Text = played.ToString
        WonText.Text = won.ToString
        LostText.Text = lost.ToString
        DrawText.Text = draw.ToString
        PercentText.Text = percent.ToString

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.Close()
    End Sub

End Class

我想你需要把这一行改成

   Dim selection As Integer
   selection = ComboBox1.SelectedIndex
SELECT…案例的语法是

        Select Case selection
            Case 1
                MsgBox("Draw.")
                draw += 1
            Case  2
                MsgBox("Paper Covers Rock. You win!")
                won += 1
            Case 3
                MsgBox("Rock Crushes Scissors. You lose.")
                lost += 1
        End Select

完美的我不得不将其更改为0 1 2,但它可以工作。非常感谢。