Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 189
  • Last Modified:

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!
0
Fl4wless
Asked:
Fl4wless
1 Solution
 
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
 
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

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now