• 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.

How could I create more than one Image object

Kvanch

Junior Member
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?
 
It sounds like you need to be giving your Image controls different names. (Meaning the "Name" or "(Name)" property.) E.g. "img1", "img2", etc.
 
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.
 
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:
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
 
Back
Top