[RESOLVED] Getting index value of an item's key in treeeview
Hi
I have a treeview on a form and I can add items hierarchically in it without any problem. I can add child nodes, remove any of them successfully. I also can use ensurevisible property to make the item's node open for the user.
My problem is that, I have the key of an item in the treeview but I don't know its index value.
What is the code to use ensurevisible property for a known key string of an item?
I don't mean something like TreeView1.Nodes(TreeView1.SelectedItem.Index).EnsureVisible.
It works for selected item but I need to use ensurevisible for an item with a known key of it.
Something like this I need
TreeView1.Nodes("key of the item").EnsureVisible
Thanks in advance
Re: Getting index value of an item's key in treeeview
I have found the solution. Here it is.
TreeView1.Nodes("key of the item").EnsureVisible works well. But you should put the item's key in quotes as shown. Obviously this is not possible during coding. You will need to use variable for the "key of the item".
The problem starts here.
Treeview1.nodes(Variable).ensurevisible syntax causes error as "invalid index value". This happens if you omit to declare the variable as string. Variant type variables in nodes collection are problematic.
So the correct usage is as follows
Dim indeks As Long, ItemKey As String
ItemKey = MUSTERILER_TABLOSU![AnahtarKendi]
indeks = TreeView1.Nodes(ItemKey).Index
TreeView1.Nodes.Item(indeks).EnsureVisible
TreeView1.Nodes.Item(indeks).Selected = True
One more thing do not forget untick the "Hide selection" option in treeview properties.
Re: [RESOLVED] Getting index value of an item's key in treeeview
That's a general rule for VB collections, not just the treeview. If you're using an unknown variant as a key, then use:
Collection(CStr(variantIndex))
to make sure it doesn't try to access by numeric index instead.
Re: [RESOLVED] Getting index value of an item's key in treeeview
The Index is an Integer, not a Long.
Just converting a numeric value to a String isn't good enough for key values. When you pass one to a method like Add it raises an "Invalid key" exception.
Just avoid "numbers" as keys entirely.
Re: [RESOLVED] Getting index value of an item's key in treeeview
Right, that's a difference between TreeView and a generic VB collection.