我有三个按钮,用户可以点击,但他们只能选择一个,他们点击的按钮会将颜色更改为浅绿色,如果他们点击其中一个按钮,他们将返回灰色。
在玩这个迷你游戏之前,他们必须选择一个按钮并在文本框中键入一个名称,但我似乎无法让代码仅用于1个按钮选项,因为无论按哪个按钮,他们都会弹出一条消息告诉他们选择一个角色我知道为什么会这样,但找不到修复请帮助
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
答案 0 :(得分:0)
尝试将Loop Until
更改为:
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)
或者更好:
Loop Until TextBox1.Text <> "" And (players(1).BackColor = Color.Aqua Or players(2).BackColor = Color.Aqua Or players(3).BackColor = Color.Aqua)
甚至更好:
Loop Until TextBox1.Text <> "" And players.Any(Function (b) b.BackColor = Color.Aqua)
答案 1 :(得分:0)
请更改&#34;或#34;到&#34;和#34;像这样
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
因为,您使用Or
。结果始终为True
,并始终显示消息。