How could I create more than one Image object

Kvanch

Junior Member
May 14, 2015
7
0
16
Hi,

I am using Visual Basic 6. I've created Image object at run-time. But when I click the Command Button again an error message appears saying:

Run-time error '727:
There is already a control with the name 'Img'

How can I bypass the error message above?
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,584
4,495
75
It sounds like you need to be giving your Image controls different names. (Meaning the "Name" or "(Name)" property.) E.g. "img1", "img2", etc.
 

BoberFett

Lifer
Oct 9, 1999
37,562
9
81
It's been a loooong while since I touched VB6, but look into control arrays. If you create a control array by giving your image control an index of 0, making it an array, you can create additional ones with the New keyword, and each new one will be added to the array.
 

Kvanch

Junior Member
May 14, 2015
7
0
16
Code:
Public Function CreateNewControl(ParentForm As Form, ClassName As String, Name As String) As Object
    Set CreateNewControl = ParentForm.Controls.Add(ClassName, Name)
End Function

Dim img(3) As Image

Private Sub Command1_Click()
Set img(0) = CreateNewControl(Form1, "vb.image", "Img0", "Img1", "Img2")

img(0) = LoadPicture("C:\buildings\colosseum\Colosseum08.gif")
img(0).Left = 4080
img(0).Top = 3960
img(0).Height = 1500
img(0).Width = 1500
img(0).Stretch = True
img(0).Visible = True
When I run the program I get this error:
Compile error:
Wrong number of arguments or invalid property assignment
 
Last edited:

BoberFett

Lifer
Oct 9, 1999
37,562
9
81
Code:
Public Function CreateNewControl(ParentForm As Form, ClassName As String, Name As String) As Object
    Set CreateNewControl = ParentForm.Controls.Add(ClassName, Name)
End Function

Dim img(3) As Image

Private Sub Command1_Click()
Set img(0) = CreateNewControl(Form1, "vb.image", "Img0", "Img1", "Img2")

img(0) = LoadPicture("C:\buildings\colosseum\Colosseum08.gif")
img(0).Left = 4080
img(0).Top = 3960
img(0).Height = 1500
img(0).Width = 1500
img(0).Stretch = True
img(0).Visible = True

You can't create multiple controls in a single call like that.

Check this documentation.

https://msdn.microsoft.com/en-us/library/aa261332(v=vs.60).aspx

https://msdn.microsoft.com/en-us/library/kxt4418a(v=vs.90).aspx