LabRat
LabRat

Reputation: 2014

Vb.net Randomising unique buttons set of buttons?

I am trying to create a login system for a touch pannel, The system shows 4 images in random order these images include a Circle, a Suare, a Triangle, and a Hexagon. The reason for displaying the images at random is to prevent the next user from identifying the previous login by finger prints left on the screen. Each of these four images may only be displayed once. I have got a working system and it is random but not random enougth the oreder of patters is frequently repeated and the last image is almost allways a Hexagon. Dose anybody know a better way of programming what I need?

       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

Upvotes: 0

Views: 43

Answers (1)

jmcilhinney
jmcilhinney

Reputation: 54427

You have overcomplicated this greatly. Just create all four Buttons in the designer, complete with images, and add them to a FlowLayoutPanel or TableLayoutPanel to get the layout you want. At run time, you can move the Buttons to random positions with little code. Here's an example that works with a 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

Here's an even simpler example that works with a 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

Just be aware that the Tab order for the Buttons will not change when their order changes using that code. You'd need something more to change that.

Upvotes: 1

Related Questions