Expand, Collapse, NodeClick Event (TreeView)

Purpose

The Expand event occurs when a Node object in a TreeView control is expanded, that is, when its child nodes become visible.

The Collapse event is generated when any Node object in a TreeView control is collapsed.

Syntax

Sub TreeView_Expand(Node As Node)

Sub TreeView_Collapse(Node As Node)

Sub TreeView_NodeClick(Node As Node)

Description

The Expand event occurs after the Click and DblClick events.

The Expand event is generated in three ways: when the user double-clicks a Node object that has child nodes; when the Expanded property for a Node object is set to True; and when the plus/minus image is clicked.

The Collapse event occurs before the standard Click event.

There are three methods of collapsing a Node: by setting the Node object's Expanded property to False, by double-clicking a Node object, and by clicking a plus/minus image when the TreeView control's Style property is set to a style that includes plus/minus images. All of these methods generate the Collapse event.

The NodeClick event occurs when a Node object is first clicked; if you continue to click it, this event does not re-occur, until you click another node and then return to it. The NodeClick event occurs before the standard Click event.

The standard Click event is generated when the user clicks any part of the TreeView control outside a node object. The NodeClick event is generated when the user clicks a particular Node object; the NodeClick event also returns a reference to a particular Node object which can be used to validate the Node before further action is taken.

Example

Ocx TreeView tv = "", 10, 10, 200, 400

tv.LineStyle = tvwRootLines

tv.Style = tvwPlusMinusText

tv.Add , , , "David"

tv.Add 1, tvwChild, , "Mary"

tv.Add 1, tvwChild, , "Harold"

tv.Add 1, tvwNext, , "Mildred"

tv.Add 4, tvwChild, , "Jennifer"

Do : Sleep  : Until Me Is Nothing

 

Sub tv_Expand(Node As Node)

If Node.Index <> 1 Then

Node.Expanded = False   ' Prevent expand.

EndIf

EndSub

 

Sub tv_Collapse(Node As Node)

If Node.Index = 1 Then

Node.Expanded = True   ' Marks it as still expanded but does not show children.

EndIf

EndSub

 

Sub tv_NodeClick(Node As Node)

If Node.Index = 1 Then

Message "Node 1 Clicked"

EndIf

EndSub

The above example causes some odd behaviour which can eventually result in branches disappearing altogether, so use with care.

See Also

TreeView, Expand, Style

{Created by Sjouke Hamstra; Last updated: 05/10/2014 by James Gaite}