random image selection in VB

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Modibbo
    New Member
    • Apr 2007
    • 33

    random image selection in VB

    Hi
    any idea how to randomly select an image/picture in vb please

    thanks
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    Originally posted by Modibbo
    Hi
    any idea how to randomly select an image/picture in vb please
    Can you be a bit more specific? What exactly do you mean by "select an image"? Give us some context to work with.

    Also, what version of VB?

    Comment

    • Modibbo
      New Member
      • Apr 2007
      • 33

      #3
      thanks
      Using VB 2005 express Edition, i'm writing a programme where a randomly selected picture (from 5), will be displayed at the click of a button.

      Here's my coding:
      Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles Button1.Click
      Dim x(4) As PictureBox
      x(0).Equals(Pic tureBox1.Image)
      x(1).Equals(Pic tureBox2.Image)
      x(2).Equals(Pic tureBox3.Image)
      x(3).Equals(Pic tureBox4.Image)
      x(4).Equals(Pic tureBox5.Image)


      Dim randomgenerator As New Random
      Dim computerchoice As Integer

      computerchoice = randomgenerator .Next(0, 5)

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        Seems reasonable so far - so what's the problem?

        Comment

        • SammyB
          Recognized Expert Contributor
          • Mar 2007
          • 807

          #5
          Originally posted by Modibbo
          thanks
          Using VB 2005 express Edition, i'm writing a programme where a randomly selected picture (from 5), will be displayed at the click of a button.

          Here's my coding:
          Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles Button1.Click
          Dim x(4) As PictureBox
          x(0).Equals(Pic tureBox1.Image)
          x(1).Equals(Pic tureBox2.Image)
          x(2).Equals(Pic tureBox3.Image)
          x(3).Equals(Pic tureBox4.Image)
          x(4).Equals(Pic tureBox5.Image)


          Dim randomgenerator As New Random
          Dim computerchoice As Integer

          computerchoice = randomgenerator .Next(0, 5)
          Where do you want the image displayed? Wouldn't it be better to keep the images in an ImageList control? Why are you creating the x array?

          If you have the images in PictureBoxes already, then you could just loop through them and set the Visibility to False except the randomly selected one.

          Comment

          • Modibbo
            New Member
            • Apr 2007
            • 33

            #6
            Originally posted by Killer42
            Seems reasonable so far - so what's the problem?

            Thanks for helping Killer42, my problem is I can't progress I don't know how to progress

            Comment

            • Modibbo
              New Member
              • Apr 2007
              • 33

              #7
              Originally posted by SammyB
              Where do you want the image displayed? Wouldn't it be better to keep the images in an ImageList control? Why are you creating the x array?

              If you have the images in PictureBoxes already, then you could just loop through them and set the Visibility to False except the randomly selected one.
              Thanks for helping Guru, I'm gonna try it now, but I've never used ImageList control and not sure how to write the code to make the randomly selected visibility false

              Comment

              • Modibbo
                New Member
                • Apr 2007
                • 33

                #8
                Originally posted by Modibbo
                Thanks for helping Guru, I'm gonna try it now, but I've never used ImageList control and not sure how to write the code to make the randomly selected visibility false
                Sorry i meant the randomly seleceted visibility TRUE

                Comment

                • Modibbo
                  New Member
                  • Apr 2007
                  • 33

                  #9
                  Hi here I still am with this programme.
                  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.


                  Public Class Form1

                  Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) 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_strop tions.GetUpperB ound(0)) {}


                  Dim m_intCount As Integer = 1
                  Dim m_strname As String


                  Dim stroutput As String
                  stroutput &= ".jpg"
                  stroutput = m_strname
                  stroutput = stroutput.Inser t(0, _
                  System.Environm ent.CurrentDire ctory & "\images\")


                  Dim getuniquerandom number As Integer
                  Dim objrandom As Random = New Random()
                  Dim intrandom As Integer
                  Do
                  intrandom = objrandom.Next( 0, m_blnUsed.Lengt h)
                  Loop Until m_blnUsed(intra ndom) = False
                  m_blnUsed(intra ndom) = True

                  intrandom = getuniquerandom number
                  m_strname = m_stroptions(in trandom)
                  Dim buildpathname() As String
                  Dim strpath As String = buildpathname()
                  PictureBox1.Ima ge = Image.FromFile( strpath)


                  End Sub
                  End Class



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

                  Comment

                  • Modibbo
                    New Member
                    • Apr 2007
                    • 33

                    #10
                    Originally posted by Modibbo
                    Hi here I still am with this programme.
                    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.


                    Public Class Form1

                    Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) 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_strop tions.GetUpperB ound(0)) {}


                    Dim m_intCount As Integer = 1
                    Dim m_strname As String


                    Dim stroutput As String
                    stroutput &= ".jpg"
                    stroutput = m_strname
                    stroutput = stroutput.Inser t(0, _
                    System.Environm ent.CurrentDire ctory & "\images\")


                    Dim getuniquerandom number As Integer
                    Dim objrandom As Random = New Random()
                    Dim intrandom As Integer
                    Do
                    intrandom = objrandom.Next( 0, m_blnUsed.Lengt h)
                    Loop Until m_blnUsed(intra ndom) = False
                    m_blnUsed(intra ndom) = True

                    intrandom = getuniquerandom number
                    m_strname = m_stroptions(in trandom)
                    Dim buildpathname() As String
                    Dim strpath As String = buildpathname()
                    PictureBox1.Ima ge = 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 on this line and area
                    Dim strpath As String = buildpathname()

                    Comment

                    • Killer42
                      Recognized Expert Expert
                      • Oct 2006
                      • 8429

                      #11
                      Originally posted by Modibbo
                      The error is on this line and area
                      Dim strpath As String = buildpathname()
                      buildpathname is an array. strPath isn't. How can you assign one to the other?

                      Comment

                      • Modibbo
                        New Member
                        • Apr 2007
                        • 33

                        #12
                        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

                        Comment

                        • SammyB
                          Recognized Expert Contributor
                          • Mar 2007
                          • 807

                          #13
                          Your code is a little screwy, but I think it will work. The major problem is that computer-generated random numbers are actually pseudo-random; that is, they generated the same set of "random" numbers each time, so since you create the random number generator in the click event, the same number is generated each time. You need to create the Random object in the form load event, so that each button push will generate the "next" random number. So, it should look like this:
                          Code:
                          Public Class Form1
                          	Private objRandom
                          	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
                          		objRandom = New Random()
                          	End Sub
                          	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

                          Comment

                          • SammyB
                            Recognized Expert Contributor
                            • Mar 2007
                            • 807

                            #14
                            Well, my code is wrong and yours is very screwy. First, we'll fix mine: the module-level definition of objRandom should be:
                            Code:
                            	Private objRandom As Random
                            Now, there are several problems in your code:
                            • You initialize m_blnUsed in the button click, so it will always be false and your loop does nothing. It should be a module-level variable (that's why you named it m_) and initialized at least in the form load
                            • stroutout & getuniquerandom number perform nothing useful
                            • When all of m_blnUsed is false, it must be reinitialized before your loop or you'll just see the five images in random order, then you're in an infinite loop.

                            Comment

                            • Modibbo
                              New Member
                              • Apr 2007
                              • 33

                              #15
                              hehehey, celebration time!!!
                              i'm there & went further on the program by your help, 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

                              Comment

                              Working...