We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Hide Picture boxes made at runtime after a selected time

Fl4wless
Fl4wless asked
on
Medium Priority
202 Views
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!
Comment
Watch Question

CERTIFIED EXPERT

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

http://www.codeproject.com/vb/net/Control_Arrays.asp
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
pain = paint
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009
Commented:
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

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.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.