Home All Groups Group Topic Archive Search About

How to create an array of images

Author
30 Jan 2006 9:06 PM
di98mase
How should I do to create an array of all my image objects found at my
form. The code below does not compile. I dont understand why?

What I want to do is to create an array of all image objects found at
my main form. I want to access these using an index into the array, but
I dont know how to allocate the array properly?

Private Sub Form_Load()

Dim imgField(1 To 3) As Image

' Initialize my array of images
imgField(1) = Image1 ' this row fails
imgField(2) = Image2
imgField(3) = Image3

' "value" is normally given from a sub routine
Select Case value
    Case 1
       imgField(ix).Picture = LoadPicture("C:\Program\Microsoft Visual
Studio\COMMON\Graphics\Bitmaps\Assorted\HAPPY.bmp")
    Case 2
       imgField(ix).Picture = LoadPicture("C:\Program\Microsoft Visual
Studio\COMMON\Graphics\Bitmaps\Assorted\key.bmp")
    Case 3
       imgField(ix).Picture = LoadPicture("C:\Program\Microsoft Visual
Studio\COMMON\Graphics\Bitmaps\Assorted\card.bmp")
    Case 4
       imgField(ix).Picture = LoadPicture("C:\Program\Microsoft Visual
Studio\COMMON\Graphics\Bitmaps\Assorted\club.bmp")
End Select



Regards,

Sebastian

Author
30 Jan 2006 9:27 PM
Jeff Johnson [MVP: VB]
"di98mase" <di98m***@hotmail.com> wrote in message
news:1138655216.000800.97150@g44g2000cwa.googlegroups.com...

> ' Initialize my array of images
> imgField(1) = Image1 ' this row fails

Try

    Set imgField(1) = Image1

But really, why not simply make the control on the form into an image array?
Author
30 Jan 2006 9:35 PM
Mike D Sutton
> How should I do to create an array of all my image objects found at my
> form. The code below does not compile. I dont understand why?
>
> What I want to do is to create an array of all image objects found at
> my main form. I want to access these using an index into the array, but
> I dont know how to allocate the array properly?
<code snipped>

A collection would be better for this kind of thing:

'***
Dim Images As Collection

Const BasePath As String = "C:\Program\Microsoft Visual Studio" & _
    "\COMMON\Graphics\Bitmaps\Assorted\"

Set Images = New Collection
Call Images.Add(LoadPicture(BasePath & "HAPPY.bmp"), "Happy")
Call Images.Add(LoadPicture(BasePath & "key.bmp"), "Key")
Call Images.Add(LoadPicture(BasePath & "card.bmp"), "Card")
Call Images.Add(LoadPicture(BasePath & "club.bmp"), "Club")
'***

You can then access images by index or key:

'***
Set Picture = Images.Item("Key")
' or
Set Picture = Images.Item(2)
'***

Hope this helps,

    Mike


- Microsoft Visual Basic MVP -
E-Mail: ED***@mvps.org
WWW: Http://EDais.mvps.org/
Author
30 Jan 2006 9:43 PM
Saga
Could you not use an image list control here?

Saga

Show quoteHide quote
"di98mase" <di98maseNON@SPAMMEhotmail.com> wrote in message
news:1138655216.000800.97150@g44g2000cwa.googlegroups.com...
> How should I do to create an array of all my image objects found at my
> form. The code below does not compile. I dont understand why?
>
> What I want to do is to create an array of all image objects found at
> my main form. I want to access these using an index into the array,
> but
> I dont know how to allocate the array properly?
>
> Private Sub Form_Load()
>
> Dim imgField(1 To 3) As Image
>
> ' Initialize my array of images
> imgField(1) = Image1 ' this row fails
> imgField(2) = Image2
> imgField(3) = Image3
>
> ' "value" is normally given from a sub routine
> Select Case value
>    Case 1
>       imgField(ix).Picture = LoadPicture("C:\Program\Microsoft Visual
> Studio\COMMON\Graphics\Bitmaps\Assorted\HAPPY.bmp")
>    Case 2
>       imgField(ix).Picture = LoadPicture("C:\Program\Microsoft Visual
> Studio\COMMON\Graphics\Bitmaps\Assorted\key.bmp")
>    Case 3
>       imgField(ix).Picture = LoadPicture("C:\Program\Microsoft Visual
> Studio\COMMON\Graphics\Bitmaps\Assorted\card.bmp")
>    Case 4
>       imgField(ix).Picture = LoadPicture("C:\Program\Microsoft Visual
> Studio\COMMON\Graphics\Bitmaps\Assorted\club.bmp")
> End Select
>
>
>
> Regards,
>
> Sebastian
>
Author
31 Jan 2006 12:55 AM
MikeD
Show quote Hide quote
"di98mase" <di98m***@hotmail.com> wrote in message
news:1138655216.000800.97150@g44g2000cwa.googlegroups.com...
> How should I do to create an array of all my image objects found at my
> form. The code below does not compile. I dont understand why?
>
> What I want to do is to create an array of all image objects found at
> my main form. I want to access these using an index into the array, but
> I dont know how to allocate the array properly?
>
> Private Sub Form_Load()
>
> Dim imgField(1 To 3) As Image
>
> ' Initialize my array of images
> imgField(1) = Image1 ' this row fails
> imgField(2) = Image2
> imgField(3) = Image3
>
> ' "value" is normally given from a sub routine
> Select Case value
>    Case 1
>       imgField(ix).Picture = LoadPicture("C:\Program\Microsoft Visual
> Studio\COMMON\Graphics\Bitmaps\Assorted\HAPPY.bmp")
>    Case 2
>       imgField(ix).Picture = LoadPicture("C:\Program\Microsoft Visual
> Studio\COMMON\Graphics\Bitmaps\Assorted\key.bmp")
>    Case 3
>       imgField(ix).Picture = LoadPicture("C:\Program\Microsoft Visual
> Studio\COMMON\Graphics\Bitmaps\Assorted\card.bmp")
>    Case 4
>       imgField(ix).Picture = LoadPicture("C:\Program\Microsoft Visual
> Studio\COMMON\Graphics\Bitmaps\Assorted\club.bmp")
> End Select


Are you needing to display all (or at least 2 or more) of these images at
once? If not, ONE Image control would suffice.  If you want to cache the
pictures, then you could use a StdPicture object and add each of those to a
collection.  Another possibility would be to create a control array of the
Image control at design-time. At runtime, just load additional Image
controls to the control array. Note that a "control array" and an "array of
controls" is NOT the same thing. What it appears you're doing is creating an
array of controls.

More information about what you're really trying to accomplish (your
ultimate goal) would be helpful. It's just not really clear why, or for what
purpose, you're wanting to do this. If we knew that purpose, we could
provide better suggestions/alternatives.

--
Mike
Microsoft MVP Visual Basic
Author
31 Jan 2006 9:40 AM
di98mase
Hi everybody,

thanks for all input to this issue. Let me describe what I intend to
do:

I am doing a Memory game where a have maximum 12 images (to start
with). So at form load I want to load an image into each Image object
but dont show any of them to start with.

So what I have done is that I have an array of 12 integers that
randomly gets an value of 1..6 every number is used twice. so it looks
like e.g. 2,1,3,5,4,6,1,2,3,4,5,6, these indexes are later used to load
an image into my Image objects, i.e. for ImageObjectArray(1) I will
load ImageArray(2). These are the steps:

1) Randomize all numbers of the playTable, these are the indexes into
all arrays e.g. ImageArray
2) for all image objects on the form load a picture corresponding to
the value found in playTable
3) image.visable = False

Do you understand what I am trying to do?