Results 1 to 9 of 9

Thread: Random PictureBox

Threaded View

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

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