简体   繁体   中英

Visual basic more than 1 button click choice in a loop

i have three buttons that the user can click but they can only choose one and the one they click on will change the color to aqua and if they had clicked one of the other buttons they would go back to gray.

before playing this mini game they have to choose a button and type a name in to a text box but i cannot seem to get the code working for the just 1 button choice because no matter which button they press a message will pop up telling them to choose a character i know why this is happening but cannot find a fix please help

Public Class Form2

    Dim players(3) As Button

    Private Sub btnwarrior_Click(sender As Object, e As EventArgs) Handles btnwarrior.Click
        btnwarrior.BackColor = Color.Aqua
        btnarcher.BackColor = Color.Gray
        btnwizard.BackColor = Color.Gray
    End Sub

    Private Sub btnarcher_Click(sender As Object, e As EventArgs) Handles btnarcher.Click
        btnwarrior.BackColor = Color.Gray
        btnarcher.BackColor = Color.Aqua
        btnwizard.BackColor = Color.Gray
    End Sub

    Private Sub btnwizard_Click(sender As Object, e As EventArgs) Handles btnwizard.Click
        btnwarrior.BackColor = Color.Gray
        btnwizard.BackColor = Color.Aqua
        btnarcher.BackColor = Color.Gray
    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        player()
        Do

            If TextBox1.Text = "" Then
                MsgBox("You must enter your name")

            End If
            If players(1).BackColor = Color.Gray = True Or players(2).BackColor = Color.Gray = True Or players(3).BackColor = Color.Gray = True Then
                MsgBox("Please choose a character")
            End If

        Loop Until TextBox1.Text <> "" And players(1).BackColor = Color.Aqua = True Or players(2).BackColor = Color.Aqua = True Or players(3).BackColor = Color.Aqua = True
        Form1.Show()
        Me.Hide()

    End Sub
    Public Sub player()
        players(1) = btnwarrior
        players(2) = btnarcher
        players(3) = btnwizard
    End Sub
End Class

Try changing the Loop Until to this:

Loop Until TextBox1.Text <> "" And (players(1).BackColor = Color.Aqua = True Or players(2).BackColor = Color.Aqua = True Or players(3).BackColor = Color.Aqua = True)

Or better yet:

Loop Until TextBox1.Text <> "" And (players(1).BackColor = Color.Aqua Or players(2).BackColor = Color.Aqua Or players(3).BackColor = Color.Aqua)

Or even better yet:

Loop Until TextBox1.Text <> "" And players.Any(Function (b) b.BackColor = Color.Aqua)

Please change "or" to "and" like this

If players(1).BackColor = Color.Gray = True And players(2).BackColor = Color.Gray = True And players(3).BackColor = Color.Gray = True Then
        MsgBox("Please choose a character")
    End If

Because, You use Or .The result will always True and always show message.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM