0
votes

I've been trying to create a code to simulate a queue for something(haven't got to that yet) for school and am trying to create multiple picture boxes and store them in a list. For some reason they are not appearing...anyone got any suggestions?

Public Class Form1
    Dim peoples As New List(Of PictureBox)()

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Timer1.Enabled = True
        Timer1.Interval = randomnumber(100, 500)
    End Sub
    Sub loopover()

        Timer1.Interval = randomnumber(100, 500)
    End Sub
    Function randomnumber(lower As Integer, upper As Integer)
        Randomize()
        Return Int((upper * Rnd()) + lower)
    End Function
    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        loopover()
        newqueuemember()
    End Sub
    Private Sub newqueuemember()

        Dim pictureBox As New PictureBox
        pictureBox.Width = 50


        pictureBox.Visible = True
        pictureBox.Height = 50
        Dim selectperson As Integer = randomnumber(1, 3)
        If selectperson = 1 Then
            pictureBox.Image = My.Resources.person1

        ElseIf selectperson = 2 Then
            pictureBox.Image = My.Resources.person2

        Else
            pictureBox.Image = My.Resources.person3

        End If
        pictureBox.Location = New Point(10, 20)
        peoples.Add(pictureBox)
    End Sub
End Class
2
You're creating PictureBoxes alright, but where in that code are you actually adding them to the form? Hint: nowhere.jmcilhinney
Simply adding them to Form won't help much without any logic or thought put into WHERE on the Form those PictureBoxes will be (they'll all be directly on top of each and you'll only see one of them). You need to come up with a way to arrange them and set their .Location property accordingly. Alternatively, you can add them to either a TabeLayoutPanel, or possibly a FlowLayoutPanel which would take care of arranging them for you: FlowLayoutPanel1.Controls.Add(pictureBox).Idle_Mind
Get rid of the randomnumber function and use the .net Random class. A single instance can be used for all the methods. At form level Private Rnd As New Random Then use like this Timer1.Interval = Rnd.Next(100, 500) Which will return a random number from 100 to 499.Mary

2 Answers

0
votes

Under:

peoples.Add(pictureBox)

add:

Me.Controls.Add(pictureBox)

Here's a C# reference (easily translated to VB):

https://support.microsoft.com/en-us/help/319266/how-to-programmatically-add-controls-to-windows-forms-at-run-time-by-u

0
votes

Use a flow layout panel, Define the size (width, height) of the objects, then simply add them to the flow layout panel, you can even have a scrollbar if the length of the list of picture boxes is longer than the height of the panel.

FlowLayoutPanel1.controls.add(picturebox_object)