• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 155
  • Last Modified:

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?
0
esotericlete
Asked:
esotericlete
  • 4
  • 3
  • 3
1 Solution
 
JohnMcCannCommented:
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
0
 
supunrCommented:
   Dim intLoopIndex As Long
    Dim index As Long
   
    ReDim boxes(100) As PictureBox
   
    For intLoopIndex = 2 To 100
       
        Set boxes(intLoopIndex) = Controls.Add("vb.picturebox", "Hex" & intLoopIndex) 'Set the box to a new control

    Next intLoopIndex

''' later on somewhere in your code

    For index = 2 To 100
        Controls.Remove "Hex" & index
        Set boxes(index) = Nothing
    Next index
0
 
supunrCommented:
ignore the line...
ReDim boxes(100) As PictureBox

I did that for my testing purposes.

Good Luck!
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
esotericleteAuthor Commented:
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 :)
0
 
supunrCommented:
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.
0
 
esotericleteAuthor Commented:
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
0
 
JohnMcCannCommented:
It's ok I also liked supunr answer.

It provided what you had asked for.
0
 
JohnMcCannCommented:
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.  

0
 
esotericleteAuthor Commented:
Wouldn't having a control array make it easier to trap events, if every control had an index?
0
 
JohnMcCannCommented:
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.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now