How to add a new node to a Sheridan Active TreeView control without popup boxes

How to add a new node to a Sheridan Active TreeView(tvw) control without popup boxes.

This does require Sheridan Controls for the .Level property of the node in the Sheridan TreeView Control. You should be able to replicate this with the standard treeview control, but you will have to come up with a different way to find out which menu to popup for the user. On a very deep tree this becomes quite complex if you are not using the Sheridan Controls. Which is why I am. I am too lazy to go into that much detail. I didn’t even do it for the program that uses this. Go ahead and populate the tvw anyway you like and add these three events.

The popAddNewNode_Click() occurs from the right Click event of the node in the tvw. The code is well commented (at least compared to my usual commenting style), so if you have any questions or want a sample program of how this works if you are having problems with this email me: [email protected]

private Sub popAddNewNode_Click()

    Dim tParent as string

    sstvwCode.SelectedItem.Expanded = false

    ' get parent node of new node to add
    tParent = sstvwCode.SelectedItem.Key

    ' Add temporary node and let user enter name for it
    set tNode = sstvwCode.Nodes.Add(tParent, ssatChild)

    ' Make sure node is selected so wrong node is not
    ' removed AfterLabelEdit
    tNode.Selected = true


End Sub

private Sub sstvwCode_AfterLabelEdit _
(Cancel as SSActiveTreeView.SSReturnBoolean, _
        NewString as string)

on error GoTo AddNodeError

    Dim nParent as string
    Dim nText as string

    ' Only proceed if user typed in new name
    If NewString <> "" then
                ' get the parent of the temp node, because it
                ' is about to be deleted
                nParent = tNode.Parent.Key
                nText = NewString
                ' Remove the temp node that the user just
                ' typed in
                sstvwCode.Nodes.Remove _
                ' Add the new node in same position
                ' with complete information
                set tNode = sstvwCode.Nodes.Add(nParent, _
                    ssatChild, nText, nText)
                ' Update the treeview
                ' This is the message box if user does not
                ' enter a name for item
                MsgBox "You must label the new item", 48, _
                        "new item not saved"
                'remove the no-named item
                sstvwCode.Nodes.Remove tNode.Index
    End If
    ' Destroy object
    set tNode = nothing

Exit Sub


    Dim iResult%

    Select Case Err.Number
        ' error occurs if name of node already exists
        Case "35602":
            iResult = MsgBox("An item with that name _
                              already exists", , _
                            "Cannot save new item")
        ' A more complete error handler truly needs to be
        ' implemented here but this is a Simple demo program,
        ' There are alot of error that can occur with the treeview.
        Case else:
            set tNode = nothing
            Exit Sub
    End Select

    set tNode = nothing

End Sub

private Sub sstvwCode_MouseUp(Button as Integer, _
    Shift as Integer, x as Single, y as Single)

    If Button = vbRightButton then
        'use the level property to avoid having error
        ' message
        'if you try to use SelectedItem.Parent.Text(or something
        ' along those lines)
        'it will generate an error that will not resume or
        ' error trap
        If sstvwCode.SelectedItem.Level = 1 Or _
           sstvwCode.SelectedItem.Level = 2 then
            me.PopupMenu popTest
            Exit Sub
        End If

    End If

End Sub

Download zipped Project File (3k)

Screen shot

More by Author

Must Read