|
-
May 22nd, 2013, 01:44 PM
#1
Thread Starter
Member
-
May 22nd, 2013, 01:54 PM
#2
Re: Random PictureBox
Well, store you picturebox's in a list(of <t>). Then use the random class to generate a random integer from 0 to that list's count in a timer's tick event with the interval set to 1000. Finally toggle that item's visibility. Here is an example, not tested, free handed:
Code:
Option Strict On
Option Explicit On
Public Class Form1
Private pb_List As New List(Of PictureBox)
Private r As New Random
Private shown As Boolean = False
Private Sub Form_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Form1.Load
'Loop through each picturebox on the form
For Each pb As PictureBox In Me.Controls.OfType(Of PictureBox)()
pb_List.Add(pb)
Next
Dim tmr As New Timer
tmr.Interval = 1000 '1 Second
'Event Handler
AddHandler tmr.Tick, AddressOf Timer_Tick
End Sub
Private Sub Timer_Tick(ByVal sender As Object, ByVal e As EventArgs)
'Static, not dimmed
Static i As Integer
If Not (shown) Then
i = r.Next(0, pb_List.Count)
End If
'shown is now what it wasn't then
shown = Not (shown)
'If shown then the pb is visible, and vise versa
pb_List.Item(i).Visible = shown
End Sub
End Class
Like I said, I free typed this and didn't test it, but it gives you an idea.
Last edited by dday9; May 22nd, 2013 at 01:56 PM.
Reason: Added comments to code
-
May 22nd, 2013, 02:04 PM
#3
Thread Starter
Member
Re: Random PictureBox
What do you mean store my picture boxes in a list of <t>???
-
May 22nd, 2013, 02:11 PM
#4
Re: Random PictureBox
 Originally Posted by Flavor Flav
What do you mean store my picture boxes in a list of <t>???
Well in my code, at the form level I declared a new instance of a general list(of <t>) in this particular case it was a list(of picturebox). In the form_load event, I add all the controls that are pictureboxs on the form to that list, I did so by calling List(Of <t>).Add, optionally if you could have used .AddRange and something along these lines:
Code:
pb_List.AddRange(Me.Controls.OfType(Of PictureBox)().ToArray)
and get rid of the for/each loop. An additional option you could do by getting rid of add and addrange is to just call the .ToList, assuming you're using .Net Framework 3.5 or higher. That would look something like this:
Code:
Private pb_List As List(Of PictureBox)
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
pb_List = Me.Controls.OfType(Of PictureBox)().ToList
End Sub
-
May 22nd, 2013, 02:44 PM
#5
Thread Starter
Member
Re: Random PictureBox
That code doesn't work for me, I changed the base of my game now though. I have 80 picture boxes and I want to make it so that it will show 4 random picture boxes every second and they will disappear every .4 seconds or if clicked.
I have NO idea how to even start this code, can someone please help me?
-
May 22nd, 2013, 02:53 PM
#6
Re: Random PictureBox
You know what, I think that it's a little discourteous for you to say 'it doesn't work' and 'NO idea where to start'. Have you tried tinkering with the code I provided? Does the code give any errors? Does it just not preform as desired? What is it? Also, you haven't once mentioned what framework you're targeting, which I think might be the issue. Either way, I'm unsubscribing from this thread. Good luck to you.
-
May 22nd, 2013, 03:28 PM
#7
Re: Random PictureBox
 Originally Posted by Flavor Flav
That code doesn't work for me, I changed the base of my game now though. I have 80 picture boxes and I want to make it so that it will show 4 random picture boxes every second and they will disappear every .4 seconds or if clicked.
I have NO idea how to even start this code, can someone please help me?
Ok lets takes this a step at a time. Where are these PictureBoxes ? Are they on a Form ? Are they inside a Panel ? Did you generate them by using code or by actually dragging them from the toolbox onto a Form/Panel/GroupBox ?
-
May 23rd, 2013, 10:48 AM
#8
Thread Starter
Member
Re: Random PictureBox
All of my picture boxes are on the form.
-
May 23rd, 2013, 04:31 PM
#9
Re: Random PictureBox
What dday wrote should work or be really close. If it didn't work for you, then what did happen? If you got an error, it will probably be easy to fix if we know what it is. If the behavior was wrong, in what way was it not what you wanted?
My usual boring signature: Nothing
 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|