• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Visual Basic: Possible to dynamically create image boxes?

Alex

Diamond Member
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?
 
Set ctl = Controls.Add("VB.Image", "img1", Form1)
With ctl
.Move 3480, 360, 1575, 375
.Picture = LoadPicture("c:\myfile.jpg")
.Visible = True
End With
 
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.
 
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?
 
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.
 
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?
 
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
 
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
 
Back
Top