Link to home
Start Free TrialLog in
Avatar of esotericlete
esotericlete

asked on

Adding objects back into a collection, after removing them.

This is the bare bones of what the code looks like (the number 100 is another variable, but was changed to a constant for simplicity's sake).


For intLoopIndex = 2 to 100
         
    Load boxes(intLoopIndex) 'Load a new box into the control array.
    Set boxes(intLoopIndex) = Hexy.Controls.Add("vb.picturebox", "Hex" & intLoopIndex) 'Set the box to a new control

End With

boxes(1) was created at design time, and is not removed until the program unloads. As you can see, as boxes are added to the array, each one is named "Hex2", "Hex3", "Hex4", and so on.

Later, when I need to start over, I use something along the lines of:

for index = 2 to 100
unload boxes(index)
next index

And, this is all fine, until I try to recreate new boxes to replace the old ones (a new array of 2 to 100). However, the compiler complains that the name "Hex2" already exists. It sounds like the boxes are being removed, but their names are still loaded in memory, or something along those lines.

How do I correct this?
Avatar of JohnMcCann
JohnMcCann

Both these lines create controls

Load boxes(intLoopIndex) 'Load a new box into the control array.

   Set boxes(intLoopIndex) = Hexy.Controls.Add("vb.picturebox", "Hex" & intLoopIndex) 'Set the box to a new control


I think you sare unloading only one set.

Would it not be better to use

Load boxes(intLoopIndex)
boxes(intLoopIndex).tag = "Hex" & intLoopIndex
ASKER CERTIFIED SOLUTION
Avatar of supunr
supunr

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
ignore the line...
ReDim boxes(100) As PictureBox

I did that for my testing purposes.

Good Luck!
Avatar of esotericlete

ASKER

supunr, What's the difference between:

Controls.Remove "Hex" & index

And:

Unload Boxes(Index) ?

And, John, that's a good point point to make, I'll certainly consider that too, provided it works with the way I'm trying to implement this :)
I am not 100% sure about that.  But I think even though you might have unloaded the pictureboxes, controls array still reference to the boxes and it does not know that boxes are already unloaded.  Similar to in C...

long* item = new long;
delete item;

// but the item is still pointing to a memory location until you type
item = NULL;

I think it is a similar concept.
Thanks for your help, it was appreciated.

And John, I found your comment very helpful as well, and is something I'll pay close attention to in future excersizes. E-mail me if you want to earn some points for your trouble :D
It's ok I also liked supunr answer.

It provided what you had asked for.
Just a thought before you go off building your rock.

Do you realise that using my approach enables you to trap events of you picture boxes like click and double click.  Where as the other approach will not as you will be creating a control array as apposed to 100 separate controls.  

Wouldn't having a control array make it easier to trap events, if every control had an index?
Yes.

And the approaxh I suggested gives you a control array.

I assume the other approach is adding them to an Array with each control havinga unique name.  In order to trap messages for these newly created controls you would need to use a technique known as subclassing (Not reccomended).  If you do not need to trap events for the controls then  it probably doesn't matter.