Results 1 to 6 of 6
  1. #1
    5 Star Lounger
    Join Date
    Jan 2001
    Location
    Vancouver, Br. Columbia, Canada
    Posts
    632
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Set node programmatically in treeview (VB 6)

    How do I set the current node programmatically - simulating what a user would do by clicking on a node?

    In particular, my program has a treeview that is built from a database recordset. Clicking a command button adds a new record to the database and to the treeview. That part works fine -- the new node appears in the treeview with the text "New", and it's entered into the database. Here's part of the code:

    If pbAddAsLeaf Then
    Me!axKeywordTree.Nodes.Add _
    "k" & rs!parentid, tvwChild, "n" & CStr(!keywordId), "New"
    Else
    Me!axKeywordTree.Nodes.Add _
    "k" & rs!parentid, tvwChild, "k" & CStr(!keywordId), "New"
    End If


    What I want to do next is to move the focus to the new keyword and have it selected, ready for editing. Found a bit of info on MSDN about moving to a node under C programming, but nothing for Visual Basic.

    Thx.
    --------------------------------------------------
    Jack MacDonald
    Vancouver, Canada

  2. #2
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Set node programmatically in treeview (VB 6)

    Hi,
    I would have said the easiest way would be to use something like:

    Dim nodCurrent as node
    Set nodCurrent = Me!axKeywordTree.Nodes.Add etc.
    nodCurrent.selected = true

    Hope that helps.
    Regards,
    Rory

    Microsoft MVP - Excel

  3. #3
    5 Star Lounger
    Join Date
    Jan 2001
    Location
    Vancouver, Br. Columbia, Canada
    Posts
    632
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Set node programmatically in treeview (VB 6)

    I've got to be doing something wrong or missing a concept, because it hasn't been easy..

    this syntax works for me:
    axKeywordTree.Nodes.Add sParentKey, tvwChild, sCurrentKey, "New"

    but it doesn't return a Node object, so I can't use the .Selected property as you suggested.

    Next, I tried this:
    Dim nNewNode as Node
    set nNewNode = axKeywordTree.Nodes.Add(sParentKey, tvwChild, sCurrentKey, "New")

    which results in Error 13, Type mismatch.

    Then I tried:
    dim vntNewNode as Variant
    set vntNewNode = axKeywordTree.Nodes.Add(sParentKey, tvwChild, sCurrentKey, "New")

    which creates the node, but doesn't *do* anything when I add:
    vntNewNode.selected = TRUE

    So, then I tried making two variables, one of type Variant, and one of type Node,

    Set nNewNode = vntNewNode
    gives the Error 13, Type mismatch


    What am I doing wrong??? The simplest setup (dim nNewNode as Node) just doesn't seem to work for me. Frustration setting in!!
    --------------------------------------------------
    Jack MacDonald
    Vancouver, Canada

  4. #4
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Set node programmatically in treeview (VB 6)

    Hi,
    I'm not sure why that syntax doesn't work for you as it does for me! The .selected property I think only works if you set the HideSelection property to No. Alternatively you can use something like this:
    <pre> Dim nodNew As Node
    Set nodNew = Me!tvwTest.Nodes.Add(, , "Test2", "This is a test")
    Me!tvwTest.DropHighlight = nodNew
    </pre>

    As a matter of interest, which version of the Treeview are you using?
    Regards,
    Rory

    Microsoft MVP - Excel

  5. #5
    5 Star Lounger
    Join Date
    Jan 2001
    Location
    Vancouver, Br. Columbia, Canada
    Posts
    632
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Set node programmatically in treeview (VB 6)

    Thanks for the reply -- I was just logging on to say that I found the problem. I have two libraries referenced in the project (dumb -- but I'm learning...). The "node" object must be referenced to the proper source library.

    Anyway,
    DIM nNewNode as ComctlLib.Node
    makes it work, whereas
    DIM nNewNode as Node
    will compile, but won't run...


    Now I will follow up with your suggestion about making the selection. Thanks for your help.
    --------------------------------------------------
    Jack MacDonald
    Vancouver, Canada

  6. #6
    5 Star Lounger
    Join Date
    Jan 2001
    Location
    Vancouver, Br. Columbia, Canada
    Posts
    632
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Set node programmatically in treeview (VB 6)

    Whew...


    Here's what I ended up with:

    Set nodCurrent = axKeywordTree.Nodes.Add(sParentKey, tvwChild, sCurrentKey, "New")

    Set axKeywordTree.SelectedItem = axKeywordTree.Nodes(sCurrentKey) ' select the node internally
    axKeywordTree.DropHighlight = nodCurrent ' highlight it
    axKeywordTree_NodeClick nodCurrent ' enable the form's command-buttons as if the user clicked the treevies
    axKeywordTree.StartLabelEdit ' start the editing process


    In addition, the _NodeClick event needs to set the .DropHight property, otherwise the user's selection does not get hightligthed.
    --------------------------------------------------
    Jack MacDonald
    Vancouver, Canada

Posting Permissions

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