Visual Basic: Possible to dynamically create image boxes?

Alex

Diamond Member
Oct 26, 1999
6,995
0
0
is it possible say if i dont know how many images i will be loading, so for every one, it creates a new image box at runtime and puts a pic in it... and so on?
 

DT4K

Diamond Member
Jan 21, 2002
6,944
3
81
Set ctl = Controls.Add("VB.Image", "img1", Form1)
With ctl
.Move 3480, 360, 1575, 375
.Picture = LoadPicture("c:\myfile.jpg")
.Visible = True
End With
 

DT4K

Diamond Member
Jan 21, 2002
6,944
3
81
I found some code on the web, made a few changes to it and got it to work.

I've actually never done this before even though I've been developing VB for a living for 3 years.

It's pretty cool though.
 

Alex

Diamond Member
Oct 26, 1999
6,995
0
0
Originally posted by: Shanti
I found some code on the web, made a few changes to it and got it to work.

I've actually never done this before even though I've been developing VB for a living for 3 years.

It's pretty cool though.

i have a question though, it says "Object Required" and points to the line starting ''Set ctl = " any ideas?
 

DT4K

Diamond Member
Jan 21, 2002
6,944
3
81
Did you replace "Form1" with the actual name of your form?
Other than that, I don't know.
I just created a new project with a single form called "Form1" and dropped the code I posted into the click event for a button and it worked.
 

DT4K

Diamond Member
Jan 21, 2002
6,944
3
81
If you want to post your complete code, I'll take a look and see if it works for me.

EDIT: We are talking about VB 6.0 here right?
 

Alex

Diamond Member
Oct 26, 1999
6,995
0
0
Originally posted by: Shanti
If you want to post your complete code, I'll take a look and see if it works for me.

EDIT: We are talking about VB 6.0 here right?

hey i didnt really change anything but it somehow just works now... except i have it in a loop to insert a collection of images but it inserts the first and then it says the name is already taken for the second one, although i made sure it changed the name after every insertion... so weird
 

Alex

Diamond Member
Oct 26, 1999
6,995
0
0
here is part of my code if anyone wants to take a look and help please:

in sub:
path = "imgUp"
frmStuff.createImageBox (path)

path = "imgDown"
frmStuff.createImageBox (path)

--

in createImageBox(longName as String):

Set ctl = Controls.Add("VB.Image", localName, frmStuff)
With ctl
.Height = 400
.Width = 400
.Stretch = True
.Move 3480, 360, 1575, 375
.Picture = LoadPicture(path)
.Visible = True
End With

--

what happens is it creates the first one, ImgUp but then on the line when it calls createImageBox for the second time it says "there is already a control with the name imgDown" which is weird because i double-checked and the first image box created is clearly named imgUp