Hide Picture boxes made at runtime after a selected time

I have a main menu that lets you choose the face time of targets.

When the 'game' is started, the targets will pop out and should stay out for the face time.

A timer calls a function and every time it is called, it creates a target box with the name pct_Target_X. Where X is an increasing integer.

I need to close these targets when they have been visible for the face time.

So if the face time is 3.0s, and pct_Target_0 appears, it will close again after 3.0s.

However, pct_Target_1 and pct_Target_2, etc can appear while pct_Target_0 is still showing. Meaning the original timer cant be used to just show 1, hide 1, show 1, hide 1.

To sum up, i need to know how to make the picture boxes invisible after they have been created after the set time. I also cannot specify this in hard code, as the number of picture boxes is completely down to the user. I wont ever know what X will get up to. I could have pct_Target_9999999999999999! Resources allowing of course :p.

Thanks in advance for help!
LVL 1
Fl4wlessAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mikal613Commented:
try using the control array to keep hold of it

http://www.codeproject.com/vb/net/Control_Arrays.asp
0
PockyMasterCommented:
Don't just hide them (you will run out of memory in the end), unload them , or re-use them.

Create a hashtable to get your pictureboxes fast.
Or just pain directly on your form, that will work as well
0
PockyMasterCommented:
pain = paint
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
You could do something like this...

    Private targets As New ArrayList

    Private Class Target
        Public pb As PictureBox
        Public dt As DateTime

        Public Sub New(ByVal p As PictureBox, ByVal d As DateTime)
            pb = p
            dt = d
        End Sub
    End Class

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim myPb As New PictureBox
        ' setup myPb...
        Me.Controls.Add(myPb)

        ' add myPb with its "close time" to our arraylist
        targets.Add(New Target(myPb, DateTime.Now.AddSeconds(3)))
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If targets.Count > 0 Then
            Dim curTarget As Target = targets.Item(0)
            If DateTime.Now.Subtract(curTarget.dt).TotalMilliseconds <= 0 Then
                Me.Controls.Remove(curTarget.pb)
                targets.RemoveAt(0)
            End If
        End If
    End Sub
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Fl4wlessAuthor Commented:
Thanks Idle_Mind! That worked a treat.

The only part I changed was the timer code:

    Private Sub tmr_Dispose_Elapsed(ByVal sender As System.Object, ByVal e As System.Timers.ElapsedEventArgs) Handles tmr_Dispose.Elapsed

        If targets.Count > 0 Then
            Dim curTarget As Target = targets.Item(0)
            Dim x As Long
            Dim y As Long
            x = curTarget.dt.Ticks
            y = DateTime.Now.Ticks

            If y >= x Then
                Me.Controls.Remove(curTarget.pb)
                targets.RemoveAt(0)
            End If
        End If

    End Sub

You're a star! Thanks chap.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.