Home > other >  Random picturebox arrangement
Random picturebox arrangement

Time:10-25

I am new to visual basics and was wondering how to do the following program: I have 9 picture boxes and a button "arrange". For my program, I would like that all picture boxes come together like a puzzle randomly to make a square that has a width and height of three picture boxes. The square made would have all nine picture boxes in one and every time you click the button "arrange" the picture boxes would change to a random location within the square. So far, I have written so that all the picture boxes become the same size but i don't know how to make them come together in a square. Thanks in advance.

Public Class frm1
    Dim Placement As Integer
Private Sub btnArrange_Click(sender As Object, e As EventArgs) Handles btnArrange.Click
    picDeux.Size = picgris.Size
    picTrois.Size = picgris.Size
    picQuatre.Size = picgris.Size
    picCinq.Size = picgris.Size
    picSix.Size = picgris.Size
    picSept.Size = picgris.Size
    picHuit.Size = picgris.Size
    picNeuf.Size = picgris.Size

    lstNum.Items.Clear()
    For i = 1 To 3
        For j = 1 To 3
            Dim L As New Point(picgris.Width * j   100, picgris.Height * i)
            lstNum.Items.Add(L)
        Next
    Next

    For i = 1 To 3
        For j = 1 To 3
            Placement = Int(Rnd() * (lstNum.Items.Count))
        Next
    Next
End Sub
End Class

CodePudding user response:

I created nine pictures boxes at design time. You would assign a different image to each picture box. They are all square and the same size. Mine are 100 x 100 to make the arithmetic easy.

I made an array of points as a form level variable. These point will form a 300 x 300 square with the picture boxes. I also declared an array of PictureBox. In the Form.Load I added the pictures boxes to the array.

To reposition the picture boxes assigned the array to a list. Items in this list will be removed because we don't want to assign the same location to more the one picture box. This will not effect the original array.

Looping through the picture boxes we assign a random position to the box then remove that point from the list.

Public Class PictureSort
    Private Rand As New Random()
    Private PointArray As Point() = {New Point(100, 100), New Point(200, 100), New Point(300, 100), New Point(100, 200), New Point(200, 200), New Point(300, 200), New Point(100, 300), New Point(200, 300), New Point(300, 300)}
    Private PictureBoxArray(8) As PictureBox

    Private Sub PictureSort_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        PictureBoxArray = {PictureBox1, PictureBox2, PictureBox3, PictureBox4, PictureBox5, PictureBox6, PictureBox7, PictureBox8, PictureBox9}
    End Sub
    Private Sub RepositionPictureBoxes()
        Dim lst = PointArray.ToList
        For Each pb In PictureBoxArray
            Dim index = Rand.Next(0, lst.Count)
            pb.Location = lst(index)
            lst.RemoveAt(index)
        Next
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        RepositionPictureBoxes()
    End Sub
End Class

CodePudding user response:

My advice is to use a control array - you have an example here that should help: Screenshot

Public Class frmPics

    Private pics As New List(Of PictureBox)
    Private Const picture_width As Integer = 100, picture_height As Integer = 50

    Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        ' instantiate controls
        Dim font As New Font("Arial", 20, FontStyle.Regular, GraphicsUnit.Pixel)

        For i As Integer = 1 To 9
            Dim pic As New PictureBox
            pic.Visible = False
            pic.Name = "pic" & i
            pic.Text = i.ToString
            Console.WriteLine("Create control: name: " & pic.Name)

            ' generate an ad-hoc bitmap image showing the index of the control
            Dim bitmap As New Bitmap(picture_width, picture_height)
            Using g As Graphics = Graphics.FromImage(bitmap)
                Dim width As Integer = CInt(g.MeasureString(Text, font).Width)
                Dim height As Integer = CInt(g.MeasureString(Text, font).Height)
            End Using
            Using g As Graphics = Graphics.FromImage(bitmap)
                g.Clear(Color.Blue)
                g.DrawString(i.ToString, font, New SolidBrush(Color.White), 0, 0)
            End Using

            pic.Image = bitmap

            pics.Add(pic)
            Me.Controls.Add(pic)

        Next

    End Sub

    Private Sub btnShuffle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShuffle.Click
        Dim x As Integer = 10, y As Integer = picture_height
        Dim counter As Integer = 1
        Dim rnd As New Random()

        ' show controls on form
        Console.WriteLine("Show controls on form")
        Me.SuspendLayout()
        For Each item In pics.OrderBy(Function() rnd.Next)
            item.Width = picture_width
            item.Height = picture_height
            item.Location = New Point(x, y)
            item.BorderStyle = BorderStyle.FixedSingle
            item.Visible = True
            Console.WriteLine("counter: " & counter & " - control name" & item.Name & " - position: " & item.Location.X & "/" & item.Location.Y & " text: " & item.Text)

            ' reset X position every 3 iterations
            If counter Mod 3 = 0 Then
                x = 10
                y  = item.Height
            Else
                x  = item.Width
            End If

            counter  = 1
        Next
        Me.ResumeLayout()

    End Sub

End Class
  • Related