Link to home
Start Free TrialLog in
Avatar of scorpi073
scorpi073

asked on

8 picture box's to random choose picture

I know theres different methods for accomplishing my idea...I'm wanting 8 picture boxes to random display out of approx 450 pictures and random change every 2 minutes.  I need some light shed on how to do this.  Being it's so many pictures, I know it should most likely be setup in a array, but I don't know how to go about setting it up and use a array to accomplish this.

experts guide me on the right path  :)

thanx
Avatar of aratani
aratani

Do you want this on a webpage? You could use this using some simple javascript.

AJ
Avatar of scorpi073

ASKER

sorry I didn't post that I'm attempting this for visual basic 6
Avatar of Mike Tomlinson
What is the layout of your form?

You could load up the file paths of all your images into an array and then pick 8 at random and load up the images into the eight pictureboxes.

I don't think it's a good idea to have all 450 pictures loaded at the same time.

Describe what your app is doing a little more and we can guide you.

~IM
Option Explicit

' Non repeating random image gallery.
'
' This code assumes a form with
' an 8 picture box control array.
'   Picture(0)
'   Picture(1)
'   Picture(2)
'   Picture(3)
'   Picture(4)
'   Picture(5)
'   Picture(6)
'   Picture(7)
'
' and it assumes a timer called Timer1
'   Timer1

Dim col1 As Collection  ' two collections to act as available and used image collections
Dim col2 As Collection

Dim acol As Collection  ' reference to current available images collection
Dim ucol As Collection  ' reference to current used images collection

Private Sub Form_Load()
    Dim t As Long
    Set col1 = New Collection
    Set col2 = New Collection
   
    ' Initialize collection of images
    ' Add path to image files to the fist collection
    ' I leave this up to you as I don't know where you
    ' are storing your files. You might load the files
    ' from disk or from a database, etc.
    For t = 1 To 450
        col1.Add "c:\test\image" & t & ".jpeg"
    Next
   
    ' Initialize our available and used references
    Set acol = col1
    Set ucol = col2
   
    ' Start our timer
    Timer1.Interval = 500
    Timer1.Enabled = True

End Sub

Private Sub Timer1_Timer()
    LoadNewSetOfImages
End Sub

Private Sub LoadNewSetOfImages()
    Dim tcol As Collection
    Dim t As Long
    Dim x As Long
   
    For t = 1 To 8
        If acol.Count = 0 Then
            ' we have exausted our available supply
            ' swap available and used collection
            Set tcol = acol
            Set acol = ucol
            Set ucol = tcol
            Set tcol = Nothing
            Beep
        End If
        x = Int(Rnd * acol.Count) + 1
       
        Picture1(t - 1).Picture = LoadPicture((acol.Item(x)))
       
        ucol.Add acol.Item(x)
        acol.Remove (x)
    Next
End Sub


Hello....
I would like to ask u...
So.. do u want to use javascript do to ur project or do u want to use the vb.net to have this random pictures display...


So... u want to auto change the images for the 45 image right?

if so...
u can try.. using this codes:
This codes are javascript:
here there is 12 images in there and will random the images want this one is used in javascript.........
<script language="JavaScript" type="text/javascript">
<!--
var beg = "<a href=\"";
var mid = "\"><img border=\"0\" src=\"";
var nex = "\" alt=\"";
var end = "\" width=\"200\" height=\"150\" border=\"0\"></a>";
var links = new Array();
var images = new Array();
var alttext = new Array();
links[0] = "http://the-URL-that-thumbnail-01-links-to.htm";
images[0] = "thumbnail01.jpg";
alttext[0] = "whatever text is for thumbnail01";
links[1] = "http://the-URL-that-thumbnail-02-links-to.htm";
images[1] = "thumbnail02.jpg";
alttext[1] = "whatever text is for thumbnail02";
links[2] = "http://the-URL-that-thumbnail-03-links-to.htm";
images[2] = "thumbnail03.jpg";
alttext[2] = "whatever text is for thumbnail03";
links[3] = "http://the-URL-that-thumbnail-04-links-to.htm";
images[3] = "thumbnail04.jpg";
alttext[3] = "whatever text is for thumbnail04";
links[4] = "http://the-URL-that-thumbnail-05-links-to.htm";
images[4] = "thumbnail05.jpg";
alttext[4] = "whatever text is for thumbnail05";
links[5] = "http://the-URL-that-thumbnail-06-links-to.htm";
images[5] = "thumbnail06.jpg";
alttext[5] = "whatever text is for thumbnail06";
links[6] = "http://the-URL-that-thumbnail-07-links-to.htm";
images[6] = "thumbnail07.jpg";
alttext[6] = "whatever text is for thumbnail07";
links[7] = "http://the-URL-that-thumbnail-08-links-to.htm";
images[7] = "thumbnail08.jpg";
alttext[7] = "whatever text is for thumbnail08";
links[8] = "http://the-URL-that-thumbnail-09-links-to.htm";
images[8] = "thumbnail09.jpg";
alttext[8] = "whatever text is for thumbnail09";
links[9] = "http://the-URL-that-thumbnail-10-links-to.htm";
images[9] = "thumbnail10.jpg";
alttext[9] = "whatever text is for thumbnail10";
links[10] = "http://the-URL-that-thumbnail-11-links-to.htm";
images[10] = "thumbnail11.jpg";
alttext[10] = "whatever text is for thumbnail11";
links[11] = "http://the-URL-that-thumbnail-12-links-to.htm";
images[11] = "thumbnail12.jpg";
alttext[11] = "whatever text is for thumbnail12";
var rannum1 = Math.floor(Math.random() * images.length)
var rannum2;
do
{
rannum2 = Math.floor(Math.random() * images.length)
}
while (rannum2 == rannum1)
var rannum3;
do
{
rannum3 = Math.floor(Math.random() * images.length)
}
while ((rannum3 == rannum1) || (rannum3 == rannum2))
//-->
</script>
thanks,....
An easy way is name your pictures 1.jpg, 2.jpg, 3.jpg and so on

put a timer on the form with interval set to 2 Mts (you can use two timers for setting that much interval)

now user Rnd function to generate a number between 1 and 442 say you get 50 then for each of the picture boxes Load the Picture 50.jpg, 50 + 1 (51.jpg) so on.

or else you can generate 8 different Random numbers.
in this case generate numbers between 8 different sets to get a better result i.e. firts no. between 1 and 50 next between 51 and 100 and so on...

Ok, recap:  I have a form that has a place to put 8 pictures, the pictures need to load random and random change every 2 minutes.  There are approx 450 pictures to random choose from.  I don't know how to setup a array that would load the picture names and load them to the picture box out of this array  

for testing code purposes, I have 3 pictures:
 c:\pic
   1.jpg
   2.jpg
   3.jpg
a form with 3 picture boxs
and the form code:

Dim randNum As Integer
Dim picpath As String

Private Sub Form_Load()
Dim arr(1 To 3) As Integer, count As Long
Dim used(1 To 3) As Boolean, rand As Integer
Randomize Timer
For count = 1 To 3
  Do
    rand = Int(Rnd * 3 + 1)
  Loop Until used(rand) = False
  used(rand) = True
  arr(count) = rand
Next count
picpath$ = "c:\pic\" & (arr(1)) & ".jpg"
Picture1.Picture = LoadPicture(picpath$)
picpath$ = "c:\pic\" & (arr(2)) & ".jpg"
Picture2.Picture = LoadPicture(picpath$)
picpath$ = "c:\pic\" & (arr(3)) & ".jpg"
Picture3.Picture = LoadPicture(picpath$)
End Sub


I can get this to work with all of my pictures, but is it the best method?
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I don't think it should be a Delete/Refund.  Several working solutions have already been posted.

~IM
I slightly disagree...

The question is not self answered... I would award Idle_Mind

Venabili
I have great respect for the responses I've been given, and I realize it isn't hard to disagree from the looks of the posts.  My view is I got no further out of the code given, I didn't even know how to implement it into my project with how much I woud have to modify or filter out.

My response to my question not being self answered:

Refer back to my question where I quote: "I know it should most likely be setup in a array, but I don't know how to go about setting it up and use a array to accomplish this"

I researched and come up with that working array code and I posted it asking if that was the best method.

The responses may be alternatives, but like I said above, I couldn't implement them into my project the way it was given, for my programming level I just needed simpler understanding of arrays and examples of using an array after it's been filled...and that's all my question was.