Results 1 to 9 of 9

Thread: Random PictureBox

  1. #1

    Thread Starter
    Member
    Join Date
    May 2013
    Posts
    57

    Random PictureBox

    How would I make it so that out of my 170 HIDDEN picture boxes, every 1 second a random one will SHOW and then HIDE?

    So basically this will happen.

    BLANK SCREEN!
    Name:  Untitled.png
Views: 553
Size:  3.6 KB
    RANDOM PICTURE BOX POPS UP!
    Name:  Untitled2.png
Views: 560
Size:  4.7 KB
    HIDES!
    Name:  Untitled.png
Views: 553
Size:  3.6 KB
    RANDOM PICTURE BOX POPS UP!
    Name:  Untitled4.png
Views: 548
Size:  4.5 KB

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,380

    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
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    Member
    Join Date
    May 2013
    Posts
    57

    Re: Random PictureBox

    What do you mean store my picture boxes in a list of <t>???

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,380

    Re: Random PictureBox

    Quote Originally Posted by Flavor Flav View Post
    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
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  5. #5

    Thread Starter
    Member
    Join Date
    May 2013
    Posts
    57

    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?

  6. #6
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,380

    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.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  7. #7
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: Random PictureBox

    Quote Originally Posted by Flavor Flav View Post
    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 ?
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  8. #8

    Thread Starter
    Member
    Join Date
    May 2013
    Posts
    57

    Re: Random PictureBox

    All of my picture boxes are on the form.

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    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
  •  



Click Here to Expand Forum to Full Width