Hide Picture boxes made at runtime after a selected time

Posted on 2006-04-05
Last Modified: 2010-05-18
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!
Question by:Fl4wless
    LVL 48

    Expert Comment

    try using the control array to keep hold of it
    LVL 14

    Expert Comment

    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
    LVL 14

    Expert Comment

    pain = paint
    LVL 85

    Accepted Solution

    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...

            ' 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
                End If
            End If
        End Sub
    LVL 1

    Author Comment

    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
                End If
            End If

        End Sub

    You're a star! Thanks chap.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now