Vb.net将按钮的唯一按钮随机化吗?

如何解决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,并添加图像,然后将它们添加到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键顺序在使用该代码更改时不会更改。您还需要更多更改它。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?