locked
Generating Random Images Help RRS feed

  • Question

  • Before I begin, I'm using this as a tool to prevent auto creation. I haven't really started codeing this part, but I can, rest assure, generate random numbers which I'll be using in a select case. I'll have to Dim the correct entry in the select...end select so it can be compaired when the user enters text into a textbox (textbox1) and presses button1. What I don't understand is how to generate the image (picturebox1), whoes location is included into the Resource/Images file. Since this prog can be installed to an unknown file for all I know, how do I get the image from this folder depending upon the random # that is generated? I hope you all follow....

    Monday, July 2, 2007 10:37 AM

Answers

  • I'm sure there is an easier way to get the images but it's beyond my knowledge at the moment

    I would think you might be able to get the images with an array and and randomly select the index. Maybe

    Either way this will work

    You just need to decide which picture you want to coincide with what number

    If you have a large number of pictures, this could get to be a lot of code so you might see what someone else has

     

    Public Class Form1

     

    Dim ranpic As Integer

    Dim randnum As Random = New Random()

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

    ranpic = randnum.Next(1, 5)

    Select Case ranpic

    Case ranpic = 1

    'picture 1 code

    my.resources.picture 1 name  

    etc...

    Case ranpic = 2

    'picture 2 code

    End Select

    End Sub

    Monday, July 2, 2007 1:54 PM

All replies

  • I'm sure there is an easier way to get the images but it's beyond my knowledge at the moment

    I would think you might be able to get the images with an array and and randomly select the index. Maybe

    Either way this will work

    You just need to decide which picture you want to coincide with what number

    If you have a large number of pictures, this could get to be a lot of code so you might see what someone else has

     

    Public Class Form1

     

    Dim ranpic As Integer

    Dim randnum As Random = New Random()

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

    ranpic = randnum.Next(1, 5)

    Select Case ranpic

    Case ranpic = 1

    'picture 1 code

    my.resources.picture 1 name  

    etc...

    Case ranpic = 2

    'picture 2 code

    End Select

    End Sub

    Monday, July 2, 2007 1:54 PM
  • Alternative solution to selecting one image from among many:  Place all the images in an ImageList control, found in the toolbox under Components.  Prefix is iml.  It will add a Component ToolTray to the bottom of your project, but it won't be seen when the program is run.  Use the Images (collection) property of the ImageList control and add your images to it.  Each of your images will be given the same name with an index (like an array) starting with Images(0).  They are not added to the Resource folder.  It is easy to add or remove images.  Select a number from among the range of indexes.  Then assign the image to the picture box using code.  For example, if the selected number variable is num:  PictureBox.Image = ImageList.Images(num) 


    Suppose you have 4 images in the ImageList collection.  In order to select one of the images at random, your code can then be:

     

          Dim randnum As Random = New Random
            Dim selnum As Integer
            selnum = randnum.Next(0, 4)

          PictureBox.Image= ImageList.Images(selnum)

     

     

    Monday, July 2, 2007 8:39 PM