Using a GUI display with a custom collection.

  • Thread starter Locke Nash Cole
  • Start date
L

Locke Nash Cole

I've made my first collection. Its a simple one but it works. Now I want
to populate a treeview from my collection to display to the user, easy
enough...

But now comes my dilemma.. say I add a context menu to the treeview item and
allow them to delete an item from the GUI how do I match the GUI up with the
collection so it deletes the item in the collection too? Cant simplly call
List.RemoveAt(index) because the GUI treeview items have no clue what the
index is in my collection... please help me :)

btw I used an inheritance of System.Collections.CollectionBase to make my
collection.
 
R

Robin Tucker

Ahhh, this requires a simple hashtable. When you add an item to your
treeview, you add it to the hashtable. I guess you want to hash
TreeViewNode to CollectionIndex.

Then, when you want to delete an item from the tree, you first delete it
from the hashtable, then the tree.
 
L

Locke Nash Cole

Robin,

I'm reading up on hashtables now.. but I've tried to learn about them before
and I think it just confused me. If you can think of an example please post
one, lemme go stick my head in MSDN now :)
 
R

Robin Tucker

Ok, they are really simple. I guess you don't need to know how they work
under the bonet to use them. A hashtable basically relates (links) one item
to another (but it does this in an efficient way)

For example, I create a hashtable thus:

Dim myHash as new HashTable

Now I want my hashtable to relate a TreeViewNode object to an integer index.
When adding nodes, I do this:

myHash.Add ( theTreeNode, theCollectionIndex )

The hashtable effectively stores a reference to the TreeNode alongside the
index (it could be any pair of Objects however, not neccessarily a tree node
and an integer).

Now, if I want to know the integer for any given tree node, I simply write:

Dim x as integer = DirectCast ( myHash ( theTreeNode ), integer )

When I want to remove a tree node, I first write:

myHash.Remove ( theTreeNode ).

Hope this helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top