• Set node programmatically in treeview (VB 6)

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Set node programmatically in treeview (VB 6)

    Author
    Topic
    #363954

    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.

    Viewing 0 reply threads
    Author
    Replies
    • #557194

      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.

      • #557729

        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!!

        • #557746

          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:

           Dim nodNew As Node
           Set nodNew = Me!tvwTest.Nodes.Add(, , "Test2", "This is a test")
           Me!tvwTest.DropHighlight = nodNew
          

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

          • #557754

            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.

            • #557764

              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.

    Viewing 0 reply threads
    Reply To: Set node programmatically in treeview (VB 6)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: