Solved

8 picture box's to random choose picture

Posted on 2004-10-04
15
463 Views
Last Modified: 2009-12-16
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
0
Comment
Question by:scorpi073
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
15 Comments
 
LVL 4

Expert Comment

by:aratani
ID: 12222590
Do you want this on a webpage? You could use this using some simple javascript.

AJ
0
 
LVL 2

Author Comment

by:scorpi073
ID: 12222754
sorry I didn't post that I'm attempting this for visual basic 6
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 12222974
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
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:SCDMETA
ID: 12223389
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

0
 
LVL 1

Expert Comment

by:fen2009
ID: 12223569

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,....
0
 
LVL 19

Expert Comment

by:arif_eqbal
ID: 12223858
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...

0
 
LVL 2

Author Comment

by:scorpi073
ID: 12230009
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?
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 200 total points
ID: 12230965
Here is one way to do it.  Create a new project and add a Timer control.  Add a PictureBox and set its Index property to 0 (zero).  Right click on the PictureBox and select Copy.  Now Right click on the Form and select Paste and reposition the control.  Repeat for however many PictureBoxes you want.  Change the picturePath variable in the Form_Load() sub to point to the folder that has your images.  In the next line, make sure the file pattern filters match what you want.

The picture selection algorithm I have given you below is not very smart.  I like the way SCDMETA approached the problem so perhaps you could incorporate his algorithm into the code below.

Regards,

Idle_Mind

Option Explicit

Private picturePath As String
Private pictures As Collection
Private nextChange As Date

Private Sub Form_Load()
    nextChange = Now()
    picturePath = "C:\Documents and Settings\Michael\My Documents\My Pictures\"
    Set pictures = getFilesInDirectory(picturePath, "*.jpg; *.bmp; *.gif")
    Randomize Timer
    Dim i As Integer
    For i = Picture1.LBound To Picture1.UBound
        Picture1(i).AutoRedraw = True
    Next i
    Timer1.Interval = 500 ' check every 1/2 second
    Timer1.Enabled = True
    Me.Show
    Me.Refresh
    Timer1_Timer
End Sub

Private Function getFilesInDirectory(targetDirectory As String, filePattern As String) As Collection
    Dim fileCollection As Collection
    Dim curFile As String
    Dim patterns As Variant
    Dim pattern As Variant
   
    Set fileCollection = New Collection
   
    On Error GoTo noSuchDirectory
    ChDir targetDirectory
   
    On Error Resume Next ' (ignore duplicate files)
    patterns = Split(filePattern, ";")
    For Each pattern In patterns
        curFile = Dir(Trim(pattern))
        Do Until curFile = ""
            fileCollection.Add curFile, curFile
            curFile = Dir()
        Loop
    Next pattern
    Set getFilesInDirectory = fileCollection
    Exit Function
   
noSuchDirectory:
    MsgBox "Invalid Directory: " & targetDirectory
    Set getFilesInDirectory = fileCollection
End Function

Private Sub Timer1_Timer()
    If Now() >= nextChange Then
        nextChange = DateAdd("n", 2, Now()) ' (2 minutes from now)

        Dim i As Integer
        Dim idx As Integer
        Dim picName As String
   
        For i = Picture1.LBound To Picture1.UBound
            idx = Int(Rnd() * pictures.Count + 1)
            picName = picturePath & pictures.Item(idx)
            Picture1(i).PaintPicture LoadPicture(picName), _
                0, 0, Picture1(i).ScaleWidth, Picture1(i).ScaleHeight, , , , , vbSrcCopy
        Next i
    End If
End Sub
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 12463725
I don't think it should be a Delete/Refund.  Several working solutions have already been posted.

~IM
0
 
LVL 20

Expert Comment

by:Venabili
ID: 12502941
I slightly disagree...

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

Venabili
0
 
LVL 2

Author Comment

by:scorpi073
ID: 12505885
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.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is about my first experience with programming Arduino.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Simple Linear Regression

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question