i am trying to create a program where user can add picture to a form at runtime..and store it in a database as well...i am using following code to define array of picturebox and load a picture in it...

Dim mimage() As PictureBox
ReDim mimage(0 To num1) As PictureBox
[B]mimage(num1).Picture = LoadPicture("path\filename.jpg")[/B]

but getting a error on the bold line - "object variable or with block variable not set"

Please help to resolve this...

First off, you have not instantiated a new picture box in itself, just a reference to a picturebox and to make things easier, I'll skip what that means and say this...

Put a picturebox on the form, change its index to zero (0), add a command button, add the following code...

Load Picture1(1)
Picture1(1).Left = Picture1(0).Left + Picture1(0).Width + 30
Picture1(1).Top = Picture1(0).Top
Picture1(1).Visible = True

Then you need to use the loadpicture function (or before visible) to put a picture in the box...

However, it looks like you are just wanting to keep an array of images in memory. If so, then you will need to change your code to this...

Dim mimage() As PictureBox
ReDim mimage(0 To num1) As PictureBox
mimage(num1).Picture = LoadPicture("path\filename.jpg")Dim mimage() As PictureBox
ReDim Preserve mimage(0 To num1) As PictureBox
Set mimage(num1).Picture = LoadPicture("path\filename.jpg")

Good Luck


why don't you use a imagelist control as picture container?
Imagelist control is in components.. Microsoft Windows Common controls 6.0 (SP6)

code for imagelist = (form with 3 pictureboxes and indexed from 1 to 3)

Dim i as integer
ImageList1.ListImages.Add , , LoadPicture("D:\xfer\Guido_Geurs_1.jpg")
ImageList1.ListImages.Add , , LoadPicture("D:\xfer\Guido_Geurs_2.jpg")
ImageList1.ListImages.Add , , LoadPicture("D:\xfer\Guido_Geurs_3.jpg")
For i = 1 To ImageList1.ListImages.Count
   Pic(i).Picture = ImageList1.ListImages(i).Picture

thank you vb5prgrmr,

but the problem seems to remain the same..i think i am doing something very basic wrong !!

to make things simpler for myself..i tried adding a single picturebox at runtime..with following code...

Private Sub Command1_Click()
Dim mimage As PictureBox
Set mimage.Picture = LoadPicture("path\image.jpg")
end sub

but still the result is same..same error !!

Please guide..

thanks ggeu for reply,

but i want to add picturebox at runtime...and cant have them beforehand on the form...and i am finding it difficult to add picturebox at runtime !!


for adding commands in VB, place a command (picturebox) with index=0 in the form and load the same command with an increment of the index like this:(see attachment)

Private Sub Command1_Click()
Load Picture1(Picture1.UBound + 1)
Picture1(Picture1.UBound).Top = Picture1.UBound * 200
Picture1(Picture1.UBound).Visible = True
End Sub



do you want the pictures selected from a filelist?



You have gone over the same information that I have posted and the OP, pan, has replied to my post that you repeated, that they cannot place a control on the form at design time and that they must create the control at run time. Which means this is the code pan is looking for...

Dim C As PictureBox
Set C = Controls.Add("VB.PictureBox", "Picture1")
C.AutoRedraw = True
C.AutoSize = True
C.Picture = LoadPicture("C:\Program Files\Microsoft Visual Studio\Common\Graphics\Bitmaps\Assorted\balloon.bmp")

C.Visible = True

Good Luck

commented: thanks +1

thanks ggeu for all the effort and help..
your solutions were useful...but i was looking for some like what vb5prgrmr gave...
sorry if i was not clear in my requirements..


thanks vb5prgrmr for the code...
this is what i was looking for...sorry if i was not very clear in my requirements earlier :)
it would be of great help if you could tell me how to use this as array now?
one more thing, i have to use 'mouse events' with the control..so as i understand, the declaration would become something like

dim WithEvents c As Picturebox

please guide me to complete this..


While this is for text boxes, it has the info you need. All you need to do is to apply it to your needs.

Good Luck


Very late into the thread... But why not Create a Control Array of Picture Boxes...? Place a PictureBox on the Form and Set Index =0 and Visible =False. Now when you want to load one more pict box, then use this code:

Dim t As Integer
t = Pict1.UBound + 1
Load Pict1(t)
Pict1(t).Visible = True
Pict1(t).Left = Pict1(t - 1).Left
Pict1(t).Top = Pict1(t - 1).Top + 300

Also, you can directly write the code in Mouse events for Pict1, checking the Index property...


Thanks 'vb5prgrmr' and 'ggeu' for helping me to understand this...

Thanks QVeen72 for suggestion...but its already been suggested by 'ggeu' and 'vb5prgrmr'