Link to home
Start Free TrialLog in
Avatar of ahammar
ahammarFlag for United States of America

asked on

is a control an item in a collection?

If I have a control on a form, is there any way I can tell if it is an item in a collection and what the collections name is?

For example:
I want to be able to click on a button and have it return false if it is not an item in a collection, and if it is an item in a  collection, then have it return which collection it is in.

Thanks and Cheers!
ahammar
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

forms.controls  collection holds all the controls on the form
if you have your own collections, you cannot tell from the control itself if it is in any collection, you have to "ask" the collection objects to check which it is in.
Why do you need this feature?  Perhaps there is an alternative we can come up with...?
Avatar of ahammar

ASKER

Hi you two...

Thanks for you comments.

This is kinda long, so don't read it if you're not interested.  This just explains why I wanted to do this.

I actually did come up with a workaround, but the reason I wanted this was because my app has 6 different controls that each display a different symbol (lines, circles, rectangles etc...) that can be dimensioned as I need them to be.  At run time I load them as I need them, shape and size them, and move them around with my mouse to form a picture.

I can select a group of them using a method called rubberbanding (thanks to Idle Mind) and move the group around.  What I wanted was to make a selection of controls, then have the option to actually make them into a permanant group so that they would always be moved together as a group as if they were 1 without having to select the entire group again each time I wanted to move them, in other words, once a selection of controls was made into a group, from that point on, they would all move together, even by moving just 1. (When I say permanant, I mean unless I ungroup them)
The only way I could think of to do this when I asked this question (without a lot of changing things) was to select the controls I wanted to turn into a group, then have each control in the selection be added to a collection, (each collection being named with a number 1 higher than the previous in case of more than 1 group was created).  Then when I clicked on 1 control that was in the collection and moved it with my mouse, it would also move the rest of the ones in the same collection with it to keep the grouped controls together.  Which meant that when I clicked on a control that was part of a collection (group) to move the group around,  I had to know the name of the collection that it was a part of, so that the code that moved the control, would know which collection of controls to move with it.  I was afraid I'd get the answer that AngelIII gave, but I had to ask.
The first idea I had before this one (which in a sense is what I ended up doing), was instead of adding each control in a selection to a collection, just setting each of their tags to an equal number.  That way they would be grouped by the number of their tag.  There was 2 reasons why I didn't want to do this.  It is slower because now my code has to check every control on the form for their tag number to see which ones to move together instead of just moving the ones in a collection, and reason 2...I was already using the tag property for something else.
So I had to add a property to each of the 6 controls (they are custom controls) to act like a second tag, and that's the way I ended up doing it.

Sorry that was so long, but it's just in case you were interested, and I am still open for suggestions if you know a way that would be faster.  The tag idea works good, but there might be a faster way.

Thanks and Cheers!

just a quick idea:

what about putting them in a picturebox of frame?
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ahammar

ASKER

Hello all...

Thank you all for your comments.
I think I will have to give the points to Idle Mind for this solution.  It works good.   It would of simplified things a lot just to be able to have a control know what collection it was an item of, but I've concluded that's not possible.  At a glance, it appears that Idle Mind's code uses a collection of collections to form the groups...with each item in a main collection being a collection of controls.  This will require a large amount of change in my app (and I've already got at least 300 hours into it), so I'm not going to get to it right away, but I am eventually going to change it over.  So I am glad to have this solution.  As I said, it works good.
It's great to have had each one of you want to help me find a solution.
I considered Mark Freeware's comment, but I couldn't figure out how to duplicate the controls that I wanted to group together into the picturebox, and have them be the same size and shape and positioned the same as they were on the form when I turned them into a group (put them in the picturebox).

I won't accept any answers here yet just in case someone else wants to show me how to do that.  Then maybe I could split points or something, otherwise, I will accept Idle Mind's solution as answer in a while.  I think he has deserved it here so far.
No matter how simple of a solution I get, it's going to take a lot of change in my app, so I still want to use whatever solution I can get that will require the least amount of change.

Thank you all!
Cheers!
ahammar
Avatar of ahammar

ASKER

Sorry for my contradiction in my last comment.  I said I was going to accept Idle Mind's solution then in the end said I was going to wait a little while.  I had a change of mind at the last minute and forgot what I had previously wrote.  I guess I gotta quit writing so much....lol

ahammar
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
My approach is twofold:

    (1) I use a Dictionary as a lookup table to tell me if a Control is part of a group.  The control is the key itself and the group name is the value.  I can see in one line if a control is part of a group:

            If isGrouped.Exists(c) Then
 
    The Exists() method tells us if the "key" (in this case the control) is present in the dictionary.  If it is, then we retrieve the controls group name:

            groupName = isGrouped.Item(c)

    (2) I then use a Collection of Collections to hold the groups using the group name as the key.  Each group is a collection that holds references to all controls in the group:

            Set group = groups.Item(groupName)

I think this is the easiest and most flexible solution.   Unfortunately, this type of behaviour isn't trivial.  By keeping the groups as collections you could add functionality that affects all controls in the group by passing the group collection to subs/functions and simply enumerating the collection inside.

I'm sure there are many other ways to do this and I would love to see how someone else would accomplish it.  =)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You guys haven't tried my code out?...It's easy enough to build and play with.

The "grouping portion" of the code is relatively small.  Most of the code there is for rubberbanding, moving controls, and copy/paste functionality...

@jim: 99% of the APIs are being used for the Rubberbanding portion.
Avatar of ahammar

ASKER

Thanks again to all of you.

Ok, I tried out the picturebox code and it works too.  The only problem is, as someone mentioned before, is that everything behind the picture box is covered and I don't think I can make the picturebox backstyle transparent.  So that won't work in my situation.  As I move the picturebox around, I would have to be able to see through it so I could see everything behind it.  But it was a good thing to learn.
Also as Idle Mind mentioned, most of the code he offered is for rubberbanding and copy/paste which I am already using.

What I would like to do is give Mark Free Software and jimbobmcgee each 50 points, and Idle Mind the other 150 for an answer that works best in my situation.

Is that good for everyone?

Thanks and Cheers!
ahammar

that should be kind of you, but i think Idle_Mind is deserving the points


good luck, and happy coding!

mark

ps Idle_Mind, will ya please take a look at this one: http://Q_21739306.html
thanx
Please give all those who participated some points...I have plenty already!   ;)