dcsimg
Results 1 to 5 of 5

Thread: [RESOLVED] Getting index value of an item's key in treeeview

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2013
    Posts
    78

    Resolved [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

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Oct 2013
    Posts
    78

    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.

  3. #3
    Lively Member
    Join Date
    Jan 2018
    Posts
    65

    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.

  4. #4
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,322

    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.
    Last edited by dilettante; Aug 16th, 2019 at 12:15 AM.

  5. #5
    Lively Member
    Join Date
    Jan 2018
    Posts
    65

    Re: [RESOLVED] Getting index value of an item's key in treeeview

    Right, that's a difference between TreeView and a generic VB collection.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width