We help IT Professionals succeed at work.

Looping through VB6 shapes in UserControl

simonwait
simonwait asked
on
Hi,

I am trying to determine which shape on my user control has been clicked.  I have tried googling but everything seems to come up with VBA which doesnt seem to be easily adaptable to this.

I'm pretty sure I'm close but its either my definition for shp or the collection I'm looping through which is wrong.

Incidentally variable a is just so I can see where I'm at as I step through.

Thanks in advance


Private Sub UserControl_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Dim shp As Shape
    For Each shp In UserControl.shapes
    a = shp.Name
    If shp.Name = "shpWorkBlock" Then
    If X >= shp.Left And X <= shp.Left + shp.Width And _
        Y >= shp.Top And Y <= shp.Top + shp.Height Then
        MsgBox shp.Index & " has been clicked."
    End If
    End If
    Next shp
End Sub

Open in new window

Comment
Watch Question

"There is still no cure for the common birthday." ~John Glenn
Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
First it looks like UserControl.shapes isn't a valid collection because the "s" in shapes isn't capitalized as it would be by VB6 if it existed.

In your Googling have you come across the lessons surrounding their ShapeLabel control like this one? It seems you have to add and raise events to do what you want.

Author

Commented:
So I'm wondering if it is wiser to put this shape into a usercontrol by itself and then loop through the usercontrols with a sub in the usercontrol to resize the shape to the usercontrol?
Martin Liss"There is still no cure for the common birthday." ~John Glenn
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
No I wouldn't think so. Just add a Click event to your shapes.

Author

Commented:
Im not sure what I have done wrong here but I dont seem to be able to create a click event for the shape.  I then tried to do my idea of putting a shape in a usercontrol and looping through the user controls but I dont seem to be able to do that either.  I cant seem to return basic information such as indexes or change the backcolor property.  Just incase its obvious to someone I will post code here.
Martin Liss"There is still no cure for the common birthday." ~John Glenn
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
Can you attach a zip file of your project?

Author

Commented:
I would but it wont allow some of my file extensions (specifically .ctl) which it found even in the zip

Ive stuck it on Wetransfer at http://we.tl/01x4AZqcJq instead.  

This usually uses a database so has some sql but if you are trying to run it then I have added an auto login button on the login page which will let you in.

The idea is that frmMain has a control ctrlSliders which in turn has multiple instances of CtrlDayBlock.  This in turn has multiple instances of a shape which I have tried 2 different ways.  Firstly as a shape which I then manipulate its size to represent different lengths of time.  Then as I was struggling with looping through those shapes I started looking at manipulating another usercontrol call ctrlBlock.  In many ways I prefer this idea because then I can make changes to just that and not have to replicate it for all of the shapes like I do in the non usercontrol version.

Thanks Martin for you help as ever
Martin Liss"There is still no cure for the common birthday." ~John Glenn
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
Sorry for the confusion but what I was looking for was the code for the OCXs you wrote which is where the Click event should be added.

Author

Commented:
Ah sorry.  That has nadenme realise I've misread the article.  I'm mid connection between Sydney and London so will take a look at some ActiveX examples when I have decent internet
Martin Liss"There is still no cure for the common birthday." ~John Glenn
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
I'm away from home too until tomorrow evening.

Author

Commented:
Great thanks Martin - it took a while but I finally got the concept.  sorry for the confusion I think I couldn't see the wood for the trees.
Martin Liss"There is still no cure for the common birthday." ~John Glenn
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
You're welcome and I'm glad I was able to help.

In my profile you'll find links to some articles I've written that may interest you.
Marty - MVP 2009 to 2014