如何解决Vb.net将按钮的唯一按钮随机化吗?
我正在尝试为触摸屏创建一个登录系统,该系统以随机顺序显示4张图像,这些图像包括一个圆,一个Suare,一个三角形和一个六角形。随机显示图像的原因是为了防止下一个用户通过屏幕上留下的指纹来识别先前的登录。 这四个图像中的每个图像只能显示一次。我有一个工作系统,它是随机的,但不是随机的,经常重复敲击图案,最后一张图像几乎总是六角形。有人知道我需要什么更好的编程方法吗?
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
,并添加图像,然后将它们添加到FlowLayoutPanel
或TableLayoutPanel
中即可获得所需的布局。在运行时,只需少量代码即可将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键顺序在使用该代码更改时不会更改。您还需要更多更改它。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。