Welcome to Dream.In.Code
Become a VB Expert!

Join 149,478 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 1,554 people online right now. Registration is fast and FREE... Join Now!




displaying random image selected

 
Reply to this topicStart new topic

displaying random image selected

Modibbo
29 Apr, 2007 - 08:22 AM
Post #1

New D.I.C Head
*

Joined: 29 Apr, 2007
Posts: 25


My Contributions
Hi there
I've got images in Resources and want to display randomly one in the picture box at the click of the button.
I've been really struggling with this for ages, help please anyone.
Here's my script and error message.
Thanks.

CODE

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim apple As String, banana As String, orange As String, pinnaple As String, mango As String
Dim m_stroptions As String() = New String() {apple, banana, orange, pinnaple, mango}

Dim m_blnUsed As Boolean() = _
New Boolean(m_stroptions.GetUpperBound(0)) {}


Dim m_intCount As Integer = 1
Dim m_strname As String


Dim stroutput As String
stroutput &= ".jpg"
stroutput = m_strname
stroutput = stroutput.Insert(0, _
System.Environment.CurrentDirectory & "\images\")


Dim getuniquerandomnumber As Integer
Dim objrandom As Random = New Random()
Dim intrandom As Integer
Do
intrandom = objrandom.Next(0, m_blnUsed.Length)
Loop Until m_blnUsed(intrandom) = False
m_blnUsed(intrandom) = True

intrandom = getuniquerandomnumber
m_strname = m_stroptions(intrandom)
Dim buildpathname() As String
Dim strpath As String = buildpathname()
PictureBox1.Image = Image.FromFile(strpath)


End Sub
End Class




Error 1 Number of indices is less than the number of dimensions of the indexed array.


The error is occuring in the line and space shown below (at the end on the brackets):
Dim strpath As String = buildpathname()[u]
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Displaying Random Image Selected
29 Apr, 2007 - 09:34 AM
Post #2

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,302



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
You have two problems, first you are using an array that has not been initialized with the paths to each of your picture files.

Simple replace "C:\path\path\file1.jpg" in each element with the correct path to each of your images. Similar to this:
CODE

Dim buildpathname() As String = {"C:\path\path\file1.jpg", "C:\path\path\file2.jpg", "C:\path\path\file1.jpg", "C:\path\path\file3.jpg"}


The next problem, which is why you are getting the error, is when you try to access the value of an array you must supply the index number of the element whose value you are trying to get.

I think this is what you meant to do, but I can't be sure with all the swapping of values you have done with the randomly generated number:
CODE

Dim strpath As String = buildpathname(intrandom)


Looks like you are accessing the other arrays correctly, I think you just forgot about that one.

Oops, just noticed you are declaring this one incorrectly, Dim m_stroptions As String() = New String() {apple, banana, orange, pinnaple, mango}

It should be declared like this, not sure why you have all of those string variables which are declared on the line before this one.
CODE

Dim m_stroptions As String() = {"apple", "banana", "orange", "pinnaple", "mango"}


If you want to provide more instruction as to the scope of your application. We can be of more help in offering suggestions for ways to improve your code.
User is online!Profile CardPM
+Quote Post

Modibbo
RE: Displaying Random Image Selected
29 Apr, 2007 - 11:24 AM
Post #3

New D.I.C Head
*

Joined: 29 Apr, 2007
Posts: 25


My Contributions
Thanks jayman9 for your help which I really appreciated.
So, after trying your suggestions here, I'm getting the error message
QUOTE
Variable 'm_strname' is used before it has been assigned a value. A null reference exception could result at runtime.

And when running the program here what I'm getting
QUOTE
FileNotFound Exception was unhandled


What I want to achieve is to display a randomly selected image (from 5 in resources) in a picturebox at the click of the button
I've been struggling with it ages and even tried to store the images in an ImageList but did not know how to randomly choose 1 image from the imagelist first and seconly after selecting it how to display it.
Any help would be so much appreciated. I should mention I've been into programming just 2 months.

Here is the new code after incorporating jayman9's suggestions
CODE
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim m_stroptions As String() = New String() {"apple", "banana", "orange", "pinnaple", "mango"}

        Dim m_blnUsed As Boolean() = _
        New Boolean(m_stroptions.GetUpperBound(0)) {}


        Dim m_intCount As Integer = 1
        Dim stroutput As String, m_strname As String
        
        stroutput = m_strname
        stroutput &= ".jpg"

        stroutput = stroutput.Insert(0, _
        System.Environment.CurrentDirectory & "\images\")


        Dim getuniquerandomnumber As Integer
        Dim objrandom As Random = New Random()
        Dim intrandom As Integer
        Do
            intrandom = objrandom.Next(0, m_blnUsed.Length)
        Loop Until m_blnUsed(intrandom) = False
        m_blnUsed(intrandom) = True

        intrandom = getuniquerandomnumber
        m_strname = m_stroptions(intrandom)
        Dim buildpathname() As String = {"C:\1st asgt resources\Resources\apple.jpg", "C:\1st asgt resources\Resources\banana.jpg", "C:\1st asgt resources\Resources\mango.jpg", "C:\1st asgt resources\Resources\orange.jpg", "C:\1st asgt resources\Resources\pinnaple.jpg"}
        Dim strpath As String = buildpathname(intrandom)
        PictureBox1.Image = Image.FromFile(strpath)


    End Sub
End Class

User is offlineProfile CardPM
+Quote Post

Modibbo
RE: Displaying Random Image Selected
29 Apr, 2007 - 11:29 AM
Post #4

New D.I.C Head
*

Joined: 29 Apr, 2007
Posts: 25


My Contributions
oops just forgot to say I'm getting
QUOTE
FileNotFound Exception was unhandled
at the last line of the code
QUOTE
PictureBox1.Image = Image.FromFile(strpath)

User is offlineProfile CardPM
+Quote Post

Jayman
RE: Displaying Random Image Selected
29 Apr, 2007 - 05:35 PM
Post #5

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,302



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
For the first error, as it states, you are trying to assign the value of m_strname to stroutput, but it does not contain a value since you haven't assigned one to it.

Assign a value to m_strname before the next line of code executes. Since I don't know what you are trying to use the variable for I cannot offer any suggestions as to what you need to store in it.
CODE

        Dim stroutput As String, m_strname As String
        
        stroutput = m_strname


The file not found error is because you must use an absolute path to where the file is located. I would suggest opening the Resource folder in Windows Explorer and copy the path from the address bar. Then just add the name of the file after the path.

As an example of a path, here is one that gets me too the Debug folder in a project I have in VS. As you can see the path is quite lengthy and depending on where the Resource folder is, yours might be lengthy as well.
C:\Documents and Settings\Jason\Desktop\School Assignments\sample files\VB.NET Programs\Test Solution 1\TestProject\bin\Debug\someFile.jpg
User is online!Profile CardPM
+Quote Post

Modibbo
RE: Displaying Random Image Selected
30 Apr, 2007 - 03:11 PM
Post #6

New D.I.C Head
*

Joined: 29 Apr, 2007
Posts: 25


My Contributions
thanks once again for all the help
i've got the code now without error but i did not achieve what i wanted: if i run it it will display always the first the first picture, so i don't think the random generator is working. to test it i proceeded by elimination and with this rest of the code, it still display the first picture
CODE
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        
        
        
        Dim intrandom As Integer

        Dim buildpathname() As String = {"C:\Documents and Settings\Mamadou\My Documents\Visual Studio 2005\Projects\1st asgt resources\1st asgt resources\Resources\banana.jpg", "C:\Documents and Settings\Mamadou\My Documents\Visual Studio 2005\Projects\1st asgt resources\1st asgt resources\Resources\mango.jpg", "C:\Documents and Settings\Mamadou\My Documents\Visual Studio 2005\Projects\1st asgt resources\1st asgt resources\Resources\orange.jpg", "C:\Documents and Settings\Mamadou\My Documents\Visual Studio 2005\Projects\1st asgt resources\1st asgt resources\Resources\pinnaple.jpg"}
        Dim strpath As String = buildpathname(intrandom)
        PictureBox1.Image = Image.FromFile(strpath)

    End Sub



here's my final code after correcting all the errors

CODE
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim m_stroptions As String() = New String() {"apple", "banana", "orange", "pinnaple", "mango"}

        Dim m_blnUsed As Boolean() = _
        New Boolean(m_stroptions.GetUpperBound(0)) {}


        Dim m_intCount As Integer = 1
        Dim stroutput As String
        stroutput &= ".jpg"

        stroutput = stroutput.Insert(0, _
        System.Environment.CurrentDirectory & "\images\")


        Dim getuniquerandomnumber(5) As Integer
        Dim objrandom As Random = New Random()
        Dim intrandom As Integer
        Do
            intrandom = objrandom.Next(0, m_blnUsed.Length)
        Loop Until m_blnUsed(intrandom) = False
        m_blnUsed(intrandom) = True

        intrandom = getuniquerandomnumber(5)
        Dim m_strname As String = m_stroptions(intrandom)
        Dim buildpathname() As String = {"C:\Documents and Settings\Mamadou\My Documents\Visual Studio 2005\Projects\1st asgt resources\1st asgt resources\Resources\apple.jpg", "C:\Documents and Settings\Mamadou\My Documents\Visual Studio 2005\Projects\1st asgt resources\1st asgt resources\Resources\banana.jpg", "C:\Documents and Settings\Mamadou\My Documents\Visual Studio 2005\Projects\1st asgt resources\1st asgt resources\Resources\mango.jpg", "C:\Documents and Settings\Mamadou\My Documents\Visual Studio 2005\Projects\1st asgt resources\1st asgt resources\Resources\orange.jpg", "C:\Documents and Settings\Mamadou\My Documents\Visual Studio 2005\Projects\1st asgt resources\1st asgt resources\Resources\pinnaple.jpg"}
        Dim strpath As String = buildpathname(intrandom)
        PictureBox1.Image = Image.FromFile(strpath)

        m_intCount += 1


so these 2 previous codes give the same result when run which is displaying always the first image on this serie of paths

comme on guys, there should be a solution to randomly select an image from resources or imagelist and then display it in a picturebox
i've been working on this more than a week now and believe me i'm suffering as a spend the whole day on it
i'm normally not a quiter, but i'm thinking of giving up now
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Displaying Random Image Selected
30 Apr, 2007 - 03:41 PM
Post #7

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,302



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
Don't give up now, especially when you are so close.

What you need to do is remove this line of code. It currently serves no purpose and is overwriting the number you need, which was randomly generated.
CODE

intrandom = getuniquerandomnumber(5)


Based on the code you supplied, I would say you are probably getting the Apple image every time. Correct?

The problem is you assign the value from getuniquerandomnumber(5) which currently does not have any values stored in it, except 0, which is the default value stored when you declared the array. In fact, since you never put any values in the getuniquerandomnumber array, every element contained in this array is 0.

Delete the line of code I mentioned and I think you will see the results you intended.

The same problem exists in the little snippet of code you provided in your first example. You've declared intrandom as a type Integer, but you never assign any values to it, so it also contains 0.

User is online!Profile CardPM
+Quote Post

Modibbo
RE: Displaying Random Image Selected
2 May, 2007 - 03:59 AM
Post #8

New D.I.C Head
*

Joined: 29 Apr, 2007
Posts: 25


My Contributions
hehehey hehe.gif thumbs-up.gif , celebration time!!!
i'm there & went further on the program by your help jayman9, i can't thank you enough for all the support and for cheering me up
last thing (promise), in design after emptying the text in properties for a label, i can't see the label anymore and i need to see it to redesign it (set the text, modify its size); but it is nowhere to be seen; so how can i get to this label in design view
User is offlineProfile CardPM
+Quote Post

Modibbo
RE: Displaying Random Image Selected
2 May, 2007 - 06:38 AM
Post #9

New D.I.C Head
*

Joined: 29 Apr, 2007
Posts: 25


My Contributions
dealt with, done now, once again tahnks a lot
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 03:50PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month