?
Solved

Adding objects back into a collection, after removing them.

Posted on 2003-03-10
10
Medium Priority
?
153 Views
Last Modified: 2010-08-05
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
Comment
Question by:esotericlete
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
10 Comments
 
LVL 5

Expert Comment

by:JohnMcCann
ID: 8108090
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
 
LVL 11

Accepted Solution

by:
supunr earned 600 total points
ID: 8108161
   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
 
LVL 11

Expert Comment

by:supunr
ID: 8108167
ignore the line...
ReDim boxes(100) As PictureBox

I did that for my testing purposes.

Good Luck!
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:esotericlete
ID: 8108262
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
 
LVL 11

Expert Comment

by:supunr
ID: 8108280
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
 

Author Comment

by:esotericlete
ID: 8108333
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
 
LVL 5

Expert Comment

by:JohnMcCann
ID: 8108572
It's ok I also liked supunr answer.

It provided what you had asked for.
0
 
LVL 5

Expert Comment

by:JohnMcCann
ID: 8108699
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
 

Author Comment

by:esotericlete
ID: 8109194
Wouldn't having a control array make it easier to trap events, if every control had an index?
0
 
LVL 5

Expert Comment

by:JohnMcCann
ID: 8109303
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses
Course of the Month9 days, 12 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question