Vb.net随机分配一组独特的按钮



我正在尝试为触摸板创建一个登录系统,该系统以随机顺序显示4个图像这些图像包括一个圆、一个三角形和一个六边形。随机显示图像的原因是为了防止下一个用户通过屏幕上留下的指纹识别上一次登录这四个图像中的每一个只能显示一次。我有一个工作系统,它是随机的,但不是随机的,因为模式的顺序经常重复,最后一个图像几乎都是六边形。有人知道更好的编程方法吗?

Private Sub Login_Load(sender As Object, e As EventArgs) Handles MyBase.Load
#Region "Create security login buttons in random order"
#Region "Generating randomisation"
Dim random As New Random()
Dim FirstButton As Integer
Dim SecondButton As Integer
Dim ThirdButton As Integer
Dim FourthButton As Integer
#Region "Sets random value for FirstButton"
FirstButton = Convert.ToString(random.Next(0, 3))
#End Region
#Region "Sets random value for SecondButton"
Do While SecondButton = FirstButton
SecondButton = Convert.ToString(random.Next(0, 3))
Loop
#End Region
#Region "Sets random value for ThirdButton"
Do While ThirdButton = FirstButton Or ThirdButton = SecondButton
ThirdButton = Convert.ToString(random.Next(0, 3))
Loop
#End Region
#Region "Sets random value for FourthButton"
Dim AssignValZero As Integer = 0
Dim AssignValOne As Integer = 1
Dim AssignValTwo As Integer = 2
Dim AssignValThree As Integer = 3
If AssignValZero = FirstButton Or SecondButton Or ThirdButton Then
If AssignValOne = FirstButton Or SecondButton Or ThirdButton Then
If AssignValTwo = FirstButton Or SecondButton Or ThirdButton Then
If AssignValThree = FirstButton Or SecondButton Or ThirdButton Then
FourthButton = AssignValThree
End If
Else
FourthButton = AssignValTwo
End If
Else
FourthButton = AssignValOne
End If
Else
FourthButton = AssignValZero
End If
#End Region
#End Region
#Region "Creating actual buttons"
#Region "Create SquareButton"
Dim SquareButton As Button = New Button
Dim ButtonSpacer As Integer = 12
SquareButton.Height = 150
SquareButton.Width = 100
SquareButton.Image = My.Resources.Square
If FirstButton = 0 Then
SquareButton.Location = New Point((0 * SquareButton.Width) + (1 * ButtonSpacer), 25)
ElseIf FirstButton = 1 Then
SquareButton.Location = New Point((1 * SquareButton.Width) + (2 * ButtonSpacer), 25)
ElseIf FirstButton = 2 Then
SquareButton.Location = New Point((2 * SquareButton.Width) + (3 * ButtonSpacer), 25)
ElseIf FirstButton = 3 Then
SquareButton.Location = New Point((3 * SquareButton.Width) + (4 * ButtonSpacer), 25)
End If
Me.Controls.Add(SquareButton)
#End Region
#Region "Create RoundButton"
Dim RoundButton As Button = New Button
RoundButton.Height = 150
RoundButton.Width = 100
RoundButton.Image = My.Resources.Circle
If SecondButton = 0 Then
RoundButton.Location = New Point((0 * RoundButton.Width) + (1 * ButtonSpacer), 25)
ElseIf SecondButton = 1 Then
RoundButton.Location = New Point((1 * RoundButton.Width) + (2 * ButtonSpacer), 25)
ElseIf SecondButton = 2 Then
RoundButton.Location = New Point((2 * RoundButton.Width) + (3 * ButtonSpacer), 25)
ElseIf SecondButton = 3 Then
RoundButton.Location = New Point((3 * RoundButton.Width) + (4 * ButtonSpacer), 25)
End If
Me.Controls.Add(RoundButton)
#End Region
#Region "Create TriangleButton"
Dim TriangleButton As Button = New Button
TriangleButton.Height = 150
TriangleButton.Width = 100
TriangleButton.Image = My.Resources.Triangle
If ThirdButton = 0 Then
TriangleButton.Location = New Point((0 * TriangleButton.Width) + (1 * ButtonSpacer), 25)
ElseIf ThirdButton = 1 Then
TriangleButton.Location = New Point((1 * TriangleButton.Width) + (2 * ButtonSpacer), 25)
ElseIf ThirdButton = 2 Then
TriangleButton.Location = New Point((2 * TriangleButton.Width) + (3 * ButtonSpacer), 25)
ElseIf ThirdButton = 3 Then
TriangleButton.Location = New Point((3 * TriangleButton.Width) + (4 * ButtonSpacer), 25)
End If
Me.Controls.Add(TriangleButton)
#End Region
#Region "Create HexagonButton"
Dim OctagonButton As Button = New Button
OctagonButton.Height = 150
OctagonButton.Width = 100
OctagonButton.Image = My.Resources.Hexagon
If FourthButton = 0 Then
OctagonButton.Location = New Point((0 * OctagonButton.Width) + (1 * ButtonSpacer), 25)
ElseIf FourthButton = 1 Then
OctagonButton.Location = New Point((1 * OctagonButton.Width) + (2 * ButtonSpacer), 25)
ElseIf FourthButton = 2 Then
OctagonButton.Location = New Point((2 * OctagonButton.Width) + (3 * ButtonSpacer), 25)
ElseIf FourthButton = 3 Then
OctagonButton.Location = New Point((3 * OctagonButton.Width) + (4 * ButtonSpacer), 25)
End If
Me.Controls.Add(OctagonButton)
#End Region
#End Region
#End Region
End Sub

您已经把这件事做得过于复杂了。只需在设计器中创建所有四个Buttons,并完成图像,然后将它们添加到FlowLayoutPanelTableLayoutPanel中即可获得所需的布局。在运行时,您可以用少量代码将Buttons移动到随机位置。下面是一个使用TableLayoutPanel:的示例

Private ReadOnly rng As New Random
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Get a list of the Buttons in the container.
Dim buttons = TableLayoutPanel1.Controls.Cast(Of Button)().ToArray()
'Get a full list of the cell positions of the Buttons and randomise it.
Dim cellPositions = buttons.Select(Function(b) TableLayoutPanel1.GetCellPosition(b)).
OrderBy(Function(tlpcp) rng.NextDouble()).
ToArray()
'Assign the random cell positions back to the Buttons.
For i = 0 To buttons.GetUpperBound(0)
TableLayoutPanel1.SetCellPosition(buttons(i), cellPositions(i))
Next
End Sub

下面是一个使用FlowLayoutPanel:的更简单的例子

Private ReadOnly rng As New Random
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim buttons = FlowLayoutPanel1.Controls.
Cast(Of Button)().
OrderBy(Function(b) rng.NextDouble()).
ToArray()
FlowLayoutPanel1.Controls.Clear()
FlowLayoutPanel1.Controls.AddRange(buttons)
End Sub

请注意,当Buttons的顺序使用该代码更改时,其Tab顺序不会更改。你需要更多的东西来改变这一点。

最新更新