Results 1 to 7 of 7

Thread: VB Express 2010 Hundreds of Pictures chosen randomly

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2002
    Location
    Sydney
    Posts
    5

    VB Express 2010 Hundreds of Pictures chosen randomly

    I want to be able to randomly choose a picture of a student from hundreds of possible ones and display it in a picture box.
    Using "Select Case intRndStudent" works fine when tested for 14 students
    Code:
       Select Case intRndStudent
                Case 1
                    picStudent.Image = My.Resources.1
                Case 2
                    picStudent.Image = My.Resources.2
    but the code will be incredibly long and adding another picture is a chore

    I want to use a variable but I can't get it to work.
    Code:
            intRndStudent = Int(Rnd() * intNumPics) + 1 ' choose a picture at random
            txtOutput.Text = intRndStudent ' used to check that random number is being chosen correctly
    
            picStudent.Image = My.Resources.intRndStudent ' this code is bad - Error	1	'intRndStudent' is not a member of 'Resources'.
    Would someone please tell me why this does not work and how to make it work?

  2. #2
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    Re: VB Express 2010 Hundreds of Pictures chosen randomly

    In my case, I have only three resource images. This code will add each of them to a list of images, which can then be loaded into a picturebox via a random number generator. See if you can adapt to your needs:

    Code:
    Public Class Form1
    
        Dim picList As New List(Of Image)
    
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Dim dEntry As DictionaryEntry
            Dim rSet As Resources.ResourceSet = My.Resources.ResourceManager.GetResourceSet _
                                            (Globalization.CultureInfo.CurrentCulture, True, True)
    
            For Each dEntry In rSet.OfType(Of Object)()
                If TypeOf (dEntry.Value) Is System.Drawing.Image Then
                    picList.Add(dEntry.Value)
                    picList(picList.Count - 1).Tag = dEntry.Key
                End If
            Next
        End Sub
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Dim r As Random = New Random
            Dim num As Integer
            Dim lowerBound As Integer
            Dim upperBound As Integer
    
            lowerBound = 0
            upperBound = 2
    
            num = r.Next(lowerBound, upperBound + 1)    'results in a number between 0 and 2, inclusive
            PictureBox1.Image = picList(num)
        End Sub
    End Class

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: VB Express 2010 Hundreds of Pictures chosen randomly

    If you're going to have mroe than just a few that you're going to choose from, then you might be better off putting the files into a folder, not as a resource... then you can use io.directory.getallfiles to get a list of the files... then create a random (note - use the random class, not the older rnd function) number between 0 and the array count (less 1 of course) to get the file name... once you have that, it's a simple matter to load it and badda bing badda boom.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

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

    Re: VB Express 2010 Hundreds of Pictures chosen randomly

    I completely agree with TG on using a folder to hold all of the files. If nothing else, having hundreds of pictures in your resources will make your application look bloated. Here is an example of doing exactly what TG said:
    Code:
    Private images As New List(Of Bitmap)
    Private r As New Random
    Private Sub MyForm_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
    
        Dim files() As String = Io.Directory.GetAllFiles("MyPath/MyImageFolder")
    
        For Each img As String In files
            images.Add(New Bitmap(img))
        Next
    
    End Sub
    
    Private Function GetRandomImage() As Bitmap
        Return images.Item(r.Next(0, images.Count))
    End Function
    "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
    New Member
    Join Date
    Apr 2002
    Location
    Sydney
    Posts
    5

    Re: VB Express 2010 Hundreds of Pictures chosen randomly

    Thankyou for your replies. I will have another go this evening and get back if I need further pointers.

  6. #6

    Thread Starter
    New Member
    Join Date
    Apr 2002
    Location
    Sydney
    Posts
    5

    Re: VB Express 2010 Hundreds of Pictures chosen randomly

    Code:
    Dim files() As String = Io.Directory.GetAllFiles("MyPath/MyImageFolder")
    VB tells me
    'GetAllFiles' is not a member of 'System.IO.Directory'

    Can you tell me why?

  7. #7
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: VB Express 2010 Hundreds of Pictures chosen randomly

    because it's GetFiles I was shooting from the hip from a system that didn't have VS.

    FYI - any time you encounter something new from the framework... if you google "MSDN {term here}" 90% of the time you'll get the documentation for it right up front. In this case "MSDN GetAllFile" brings up "Did you mean 'GetFiles'?" and a bunch of links to the documentation for Directory.GetFiles. The first of which is the one I linked to above.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

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