user3389547
user3389547

Reputation: 33

VB.Net ComboBox.SelectedIndex always reverts to 1

I am writing a Rock-Paper-Scissors game and have to use a ComboBox to get the user's selection. The problem is that no matter what I select, the selected index always reverts to 1, thus the user always selects rock. How can I fix this?

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

Upvotes: 1

Views: 335

Answers (1)

Steve
Steve

Reputation: 216293

I think you need to change this line to

   Dim selection As Integer
   selection = ComboBox1.SelectedIndex

and the syntax for the SELECT....CASE is

        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

Upvotes: 1

Related Questions