TreeView Control
Provides a hierarchical tree display of data, supporting node expand/collapse, checkboxes, multi-select, image lists, label editing, sorting, and OLE drag-and-drop.
Enumerations
TvwStyleConstants
Tree view style constants.
| Constant | Value | Description |
|---|---|---|
| TvwStyleTextOnly | 0 | Text only |
| TvwStylePictureText | 1 | Picture and text |
| TvwStylePlusMinusText | 2 | Plus/minus signs and text |
| TvwStylePlusMinusPictureText | 3 | Plus/minus signs, picture, and text |
| TvwStyleTreeLinesText | 4 | Tree lines and text |
| TvwStyleTreeLinesPictureText | 5 | Tree lines, picture, and text |
| TvwStyleTreeLinesPlusMinusText | 6 | Tree lines, plus/minus signs, and text |
| TvwStyleTreeLinesPlusMinusPictureText | 7 | Tree lines, plus/minus signs, picture, and text |
TvwLineStyleConstants
Line style constants.
| Constant | Value | Description |
|---|---|---|
| TvwLineStyleTreeLines | 0 | Show tree lines |
| TvwLineStyleRootLines | 1 | Show root lines (lines shown between root nodes as well) |
TvwLabelEditConstants
Label edit mode constants.
| Constant | Value | Description |
|---|---|---|
| TvwLabelEditAutomatic | 0 | Automatic label edit (click to select, click again to edit) |
| TvwLabelEditManual | 1 | Manual label edit (requires code to call StartLabelEdit) |
| TvwLabelEditDisabled | 2 | Label edit disabled |
TvwNodeRelationshipConstants
Node relationship constants, used by the Add and Move methods.
| Constant | Value | Description |
|---|---|---|
| TvwNodeFirst | 0 | First node |
| TvwNodeLast | 1 | Last node |
| TvwNodeNext | 2 | Next node (same level) |
| TvwNodePrevious | 3 | Previous node (same level) |
| TvwNodeChild | 4 | Child node |
TvwSortOrderConstants
Sort order constants.
| Constant | Value | Description |
|---|---|---|
| TvwSortAscending | 0 | Ascending order |
| TvwSortDescending | 1 | Descending order |
TvwSortTypeConstants
Sort type constants.
| Constant | Value | Description |
|---|---|---|
| TvwSortBinary | 0 | Binary sort (case-sensitive) |
| TvwSortText | 1 | Text sort (case-insensitive) |
TvwMultiSelectConstants
Multi-select mode constants.
| Constant | Value | Description |
|---|---|---|
| TvwMultiSelectNone | 0 | Multi-select not allowed |
| TvwMultiSelectAll | 1 | Allow selecting all nodes |
| TvwMultiSelectVisibleOnly | 2 | Allow selecting visible nodes only |
| TvwMultiSelectRestrictSiblings | 3 | Allow selecting sibling nodes only |
TvwVisualThemeConstants
Visual theme constants.
| Constant | Value | Description |
|---|---|---|
| TvwVisualThemeStandard | 0 | Standard theme |
| TvwVisualThemeExplorer | 1 | Explorer theme |
Properties
Name
Public Property Get Name() As StringReturns the name used to identify the object in code.
Tag
Public Property Get Tag() As String
Public Property Let Tag(ByVal Value As String)Stores extra data needed by the program.
Parent
Public Property Get Parent() As ObjectReturns the object that contains this object.
Container
Public Property Get Container() As Object
Public Property Set Container(ByVal Value As Object)Returns/sets the object's container.
Left
Public Property Get Left() As Single
Public Property Let Left(ByVal Value As Single)Returns/sets the distance between the object and the left edge of its container.
Top
Public Property Get Top() As Single
Public Property Let Top(ByVal Value As Single)Returns/sets the distance between the object and the top edge of its container.
Width
Public Property Get Width() As Single
Public Property Let Width(ByVal Value As Single)Returns/sets the width of the object.
Height
Public Property Get Height() As Single
Public Property Let Height(ByVal Value As Single)Returns/sets the height of the object.
Visible
Public Property Get Visible() As Boolean
Public Property Let Visible(ByVal Value As Boolean)Returns/sets whether the object is visible.
ToolTipText
Public Property Get ToolTipText() As String
Public Property Let ToolTipText(ByVal Value As String)Returns/sets the tooltip text displayed when the mouse hovers over the control.
HelpContextID
Public Property Get HelpContextID() As Long
Public Property Let HelpContextID(ByVal Value As Long)Returns/sets the help context ID.
WhatsThisHelpID
Public Property Get WhatsThisHelpID() As Long
Public Property Let WhatsThisHelpID(ByVal Value As Long)Returns/sets the associated context-sensitive help ID.
Align
Public Property Get Align() As Integer
Public Property Let Align(ByVal Value As Integer)Returns/sets the alignment of the control on its form.
DragIcon
Public Property Get DragIcon() As IPictureDisp
Public Property Let DragIcon(ByVal Value As IPictureDisp)
Public Property Set DragIcon(ByVal Value As IPictureDisp)Returns/sets the icon displayed during a drag-and-drop operation.
DragMode
Public Property Get DragMode() As Integer
Public Property Let DragMode(ByVal Value As Integer)Returns/sets the drag mode.
hWnd
Public Property Get hWnd() As LongPtrReturns the control handle.
hWndUserControl
Public Property Get hWndUserControl() As LongPtrReturns the UserControl handle.
hWndLabelEdit
Public Property Get hWndLabelEdit() As LongPtrReturns the label edit box handle.
Font
Public Property Get Font() As StdFont
Public Property Let Font(ByVal NewFont As StdFont)
Public Property Set Font(ByVal NewFont As StdFont)Returns/sets the font.
VisualStyles
Public Property Get VisualStyles() As Boolean
Public Property Let VisualStyles(ByVal Value As Boolean)Returns/sets whether visual styles are enabled. Requires comctl32.dll 6.0 or later.
VisualTheme
Public Property Get VisualTheme() As TvwVisualThemeConstants
Public Property Let VisualTheme(ByVal Value As TvwVisualThemeConstants)Returns/sets the visual theme.
Enabled
Public Property Get Enabled() As Boolean
Public Property Let Enabled(ByVal Value As Boolean)Returns/sets whether the object can respond to user-generated events.
OLEDragMode
Public Property Get OLEDragMode() As VBRUN.OLEDragConstants
Public Property Let OLEDragMode(ByVal Value As VBRUN.OLEDragConstants)Returns/sets the OLE drag mode.
OLEDragDropScroll
Public Property Get OLEDragDropScroll() As Boolean
Public Property Let OLEDragDropScroll(ByVal Value As Boolean)Returns/sets whether automatic scrolling is enabled during OLE drag-and-drop.
OLEDragExpandTime
Public Property Get OLEDragExpandTime() As Long
Public Property Let OLEDragExpandTime(ByVal Value As Long)Returns/sets the hover duration before expanding a node during OLE drag-and-drop (milliseconds).
OLEDropMode
Public Property Get OLEDropMode() As OLEDropModeConstants
Public Property Let OLEDropMode(ByVal Value As OLEDropModeConstants)Returns/sets whether the object can act as an OLE drop target.
MousePointer
Public Property Get MousePointer() As CCMousePointerConstants
Public Property Let MousePointer(ByVal Value As CCMousePointerConstants)Returns/sets the pointer type displayed when the mouse hovers over the control. See common enumerations.
MouseIcon
Public Property Get MouseIcon() As IPictureDisp
Public Property Let MouseIcon(ByVal Value As IPictureDisp)
Public Property Set MouseIcon(ByVal Value As IPictureDisp)Returns/sets the custom mouse icon.
MouseTrack
Public Property Get MouseTrack() As Boolean
Public Property Let MouseTrack(ByVal Value As Boolean)Returns/sets whether events are raised when the mouse enters or leaves the control.
RightToLeft
Public Property Get RightToLeft() As Boolean
Public Property Let RightToLeft(ByVal Value As Boolean)Returns/sets the right-to-left display direction.
RightToLeftLayout
Public Property Get RightToLeftLayout() As Boolean
Public Property Let RightToLeftLayout(ByVal Value As Boolean)Returns/sets the right-to-left layout.
RightToLeftMode
Public Property Get RightToLeftMode() As CCRightToLeftModeConstants
Public Property Let RightToLeftMode(ByVal Value As CCRightToLeftModeConstants)Returns/sets the right-to-left mode. See common enumerations.
ImageList
Public Property Get ImageList() As Variant
Public Property Let ImageList(ByVal Value As Variant)
Public Property Set ImageList(ByVal Value As Variant)Returns/sets the associated ImageList control. Can be an object reference, a string key name, or a LongPtr handle.
BorderStyle
Public Property Get BorderStyle() As CCBorderStyleConstants
Public Property Let BorderStyle(ByVal Value As CCBorderStyleConstants)Returns/sets the border style. See common enumerations.
BackColor
Public Property Get BackColor() As OLE_COLOR
Public Property Let BackColor(ByVal Value As OLE_COLOR)Returns/sets the background color.
ForeColor
Public Property Get ForeColor() As OLE_COLOR
Public Property Let ForeColor(ByVal Value As OLE_COLOR)Returns/sets the foreground color.
Redraw
Public Property Get Redraw() As Boolean
Public Property Let Redraw(ByVal Value As Boolean)Returns/sets whether redrawing is enabled. Disabling can accelerate bulk operations.
Style
Public Property Get Style() As TvwStyleConstants
Public Property Let Style(ByVal Value As TvwStyleConstants)Returns/sets the tree view style.
LineStyle
Public Property Get LineStyle() As TvwLineStyleConstants
Public Property Let LineStyle(ByVal Value As TvwLineStyleConstants)Returns/sets the line style.
LineColor
Public Property Get LineColor() As OLE_COLOR
Public Property Let LineColor(ByVal Value As OLE_COLOR)Returns/sets the line color.
LabelEdit
Public Property Get LabelEdit() As TvwLabelEditConstants
Public Property Let LabelEdit(ByVal Value As TvwLabelEditConstants)Returns/sets the label edit mode.
Checkboxes
Public Property Get Checkboxes() As Boolean
Public Property Let Checkboxes(ByVal Value As Boolean)Returns/sets whether checkboxes are displayed next to nodes.
ShowTips
Public Property Get ShowTips() As Boolean
Public Property Let ShowTips(ByVal Value As Boolean)Returns/sets whether tooltips are displayed.
HideSelection
Public Property Get HideSelection() As Boolean
Public Property Let HideSelection(ByVal Value As Boolean)Returns/sets whether the selection highlight is hidden when the control loses focus.
FullRowSelect
Public Property Get FullRowSelect() As Boolean
Public Property Let FullRowSelect(ByVal Value As Boolean)Returns/sets whether full row selection is enabled.
HotTracking
Public Property Get HotTracking() As Boolean
Public Property Let HotTracking(ByVal Value As Boolean)Returns/sets whether hot tracking is enabled.
Indentation
Public Property Get Indentation() As Single
Public Property Let Indentation(ByVal Value As Single)Returns/sets the child node indentation amount.
PathSeparator
Public Property Get PathSeparator() As String
Public Property Let PathSeparator(ByVal Value As String)Returns/sets the path separator used by the FullPath property.
Scroll
Public Property Get Scroll() As Boolean
Public Property Let Scroll(ByVal Value As Boolean)Returns/sets whether scroll bars are displayed.
SingleSel
Public Property Get SingleSel() As Boolean
Public Property Let SingleSel(ByVal Value As Boolean)Returns/sets whether clicking a node expands it and collapses other nodes.
Sorted
Public Property Get Sorted() As Boolean
Public Property Let Sorted(ByVal Value As Boolean)Returns/sets whether root nodes are sorted.
SortOrder
Public Property Get SortOrder() As TvwSortOrderConstants
Public Property Let SortOrder(ByVal Value As TvwSortOrderConstants)Returns/sets the sort order.
SortType
Public Property Get SortType() As TvwSortTypeConstants
Public Property Let SortType(ByVal Value As TvwSortTypeConstants)Returns/sets the sort type.
InsertMarkColor
Public Property Get InsertMarkColor() As OLE_COLOR
Public Property Let InsertMarkColor(ByVal Value As OLE_COLOR)Returns/sets the insert mark color.
DoubleBuffer
Public Property Get DoubleBuffer() As Boolean
Public Property Let DoubleBuffer(ByVal Value As Boolean)Returns/sets whether double-buffered drawing is enabled.
IMEMode
Public Property Get IMEMode() As CCIMEModeConstants
Public Property Let IMEMode(ByVal Value As CCIMEModeConstants)Returns/sets the input method editor mode. See common enumerations.
MultiSelect
Public Property Get MultiSelect() As TvwMultiSelectConstants
Public Property Let MultiSelect(ByVal Value As TvwMultiSelectConstants)Returns/sets the multi-select mode.
Nodes
Public Property Get Nodes() As TvwNodesReturns the nodes collection.
Methods
OLEDrag
Public Sub OLEDrag()Initiates an OLE drag-and-drop operation.
Drag
Public Sub Drag(Optional ByRef Action As Variant)Starts, ends, or cancels a drag operation.
SetFocus
Public Sub SetFocus()Moves focus to the specified object.
ZOrder
Public Sub ZOrder(Optional ByRef Position As Variant)Sets the Z-order.
Refresh
Public Sub Refresh()Forces a complete redraw of the object.
Events
Click
Public Event Click()Raised when the user clicks the control.
DblClick
Public Event DblClick()Raised when the user double-clicks the control.
NodeClick
Public Event NodeClick(ByVal Node As TvwNode, ByVal Button As Integer)Raised when the user clicks a node. Button indicates the mouse button.
NodeDblClick
Public Event NodeDblClick(ByVal Node As TvwNode, ByVal Button As Integer)Raised when the user double-clicks a node.
NodeBeforeCheck
Public Event NodeBeforeCheck(ByVal Node As TvwNode, ByRef Cancel As Boolean)Raised before a node's checkbox is about to change. Set Cancel to True to cancel the operation.
NodeCheck
Public Event NodeCheck(ByVal Node As TvwNode)Raised after a node's checkbox state has changed.
NodeDrag
Public Event NodeDrag(ByVal Node As TvwNode, ByVal Button As Integer)Raised when the user drags a node.
NodeBeforeSelect
Public Event NodeBeforeSelect(ByVal Node As TvwNode, ByRef Cancel As Boolean)Raised before a node is about to be selected. Set Cancel to True to cancel the selection.
NodeSelect
Public Event NodeSelect(ByVal Node As TvwNode)Raised after a node has been selected.
NodeRangeSelect
Public Event NodeRangeSelect(ByVal Node As TvwNode, ByRef Cancel As Boolean)Raised during a range selection. Set Cancel to True to cancel.
BeforeCollapse
Public Event BeforeCollapse(ByVal Node As TvwNode, ByRef Cancel As Boolean)Raised before a node is about to collapse. Set Cancel to True to cancel the collapse.
Collapse
Public Event Collapse(ByVal Node As TvwNode)Raised after a node has collapsed.
BeforeExpand
Public Event BeforeExpand(ByVal Node As TvwNode, ByRef Cancel As Boolean)Raised before a node is about to expand. Set Cancel to True to cancel the expansion.
Expand
Public Event Expand(ByVal Node As TvwNode)Raised after a node has expanded.
BeforeLabelEdit
Public Event BeforeLabelEdit(ByRef Cancel As Boolean)Raised before a label is about to be edited. Set Cancel to True to cancel the edit.
AfterLabelEdit
Public Event AfterLabelEdit(ByRef Cancel As Boolean, ByRef NewString As String)Raised after label editing is complete. Set Cancel to True to cancel the modification; NewString is the edited text.
PreviewKeyDown
Public Event PreviewKeyDown(ByVal KeyCode As Integer, ByRef IsInputKey As Boolean)Key preview event. Set IsInputKey to True to mark the key as an input key.
PreviewKeyUp
Public Event PreviewKeyUp(ByVal KeyCode As Integer, ByRef IsInputKey As Boolean)Key release preview event.
KeyDown
Public Event KeyDown(KeyCode As Integer, Shift As Integer)Raised when a keyboard key is pressed.
KeyUp
Public Event KeyUp(KeyCode As Integer, Shift As Integer)Raised when a keyboard key is released.
KeyPress
Public Event KeyPress(KeyChar As Integer)Raised when an ANSI key is pressed and released.
MouseDown
Public Event MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)Raised when a mouse button is pressed.
MouseMove
Public Event MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)Raised when the mouse is moved.
MouseUp
Public Event MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)Raised when a mouse button is released.
MouseEnter
Public Event MouseEnter()Raised when the mouse enters the control area.
MouseLeave
Public Event MouseLeave()Raised when the mouse leaves the control area.
OLECompleteDrag
Public Event OLECompleteDrag(Effect As Long)Raised when an OLE drag-and-drop operation is completed.
OLEDragDrop
Public Event OLEDragDrop(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single)Raised when an OLE drag-and-drop operation is dropped.
OLEDragOver
Public Event OLEDragOver(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single, State As Integer)Raised during an OLE drag-and-drop hover.
OLEGiveFeedback
Public Event OLEGiveFeedback(Effect As Long, DefaultCursors As Boolean)Raised when OLE drag-and-drop gives feedback.
OLESetData
Public Event OLESetData(Data As DataObject, DataFormat As Integer)Raised when OLE drag-and-drop sets data.
OLEStartDrag
Public Event OLEStartDrag(Data As DataObject, AllowedEffects As Long)Raised when an OLE drag-and-drop operation starts.
Sub-Objects
TvwNode Class
Tree view node object.
TvwNode Properties
Index
Public Property Get Index() As LongThe index of the node in the collection.
Key
Public Property Get Key() As String
Public Property Let Key(ByVal Value As String)Unique identifier key.
Tag
Public Property Get Tag() As Variant
Public Property Let Tag(ByVal Value As Variant)Extra data.
Handle
Public Property Get Handle() As LongPtrNode handle.
Text
Public Property Get Text() As String
Public Property Let Text(ByVal Value As String)Node text.
ToolTipText
Public Property Get ToolTipText() As String
Public Property Let ToolTipText(ByVal Value As String)ToolTip text.
BackColor
Public Property Get BackColor() As OLE_COLOR
Public Property Let BackColor(ByVal Value As OLE_COLOR)Background color.
ForeColor
Public Property Get ForeColor() As OLE_COLOR
Public Property Let ForeColor(ByVal Value As OLE_COLOR)Foreground color.
Image
Public Property Get Image() As Variant
Public Property Let Image(ByVal Value As Variant)Node image.
ImageIndex
Public Property Get ImageIndex() As LongImage index.
SelectedImage
Public Property Get SelectedImage() As Variant
Public Property Let SelectedImage(ByVal Value As Variant)Selected state image.
SelectedImageIndex
Public Property Get SelectedImageIndex() As LongSelected image index.
ExpandedImage
Public Property Get ExpandedImage() As Variant
Public Property Let ExpandedImage(ByVal Value As Variant)Expanded state image.
ExpandedImageIndex
Public Property Get ExpandedImageIndex() As LongExpanded image index.
NoImages
Public Property Get NoImages() As Boolean
Public Property Let NoImages(ByVal Value As Boolean)Whether no image is displayed.
Selected
Public Property Get Selected() As Boolean
Public Property Let Selected(ByVal Value As Boolean)Whether selected.
CheckBox
Public Property Get CheckBox() As Boolean
Public Property Let CheckBox(ByVal Value As Boolean)Whether a checkbox is displayed.
Checked
Public Property Get Checked() As Boolean
Public Property Let Checked(ByVal Value As Boolean)Whether checked.
Bold
Public Property Get Bold() As Boolean
Public Property Let Bold(ByVal Value As Boolean)Whether bold.
Ghosted
Public Property Get Ghosted() As Boolean
Public Property Let Ghosted(ByVal Value As Boolean)Whether ghosted.
Expanded
Public Property Get Expanded() As Boolean
Public Property Let Expanded(ByVal Value As Boolean)Whether expanded.
Enabled
Public Property Get Enabled() As Boolean
Public Property Let Enabled(ByVal Value As Boolean)Whether enabled.
Sorted
Public Property Get Sorted() As Boolean
Public Property Let Sorted(ByVal Value As Boolean)Whether child nodes are sorted.
SortOrder
Public Property Get SortOrder() As TvwSortOrderConstants
Public Property Let SortOrder(ByVal Value As TvwSortOrderConstants)Sort order.
SortType
Public Property Get SortType() As TvwSortTypeConstants
Public Property Let SortType(ByVal Value As TvwSortTypeConstants)Sort type.
Children
Public Property Get Children() As LongNumber of child nodes.
Child
Public Property Get Child() As TvwNodeFirst child node.
Level
Public Property Get Level() As LongNode level (root node is 0).
Parent
Public Property Get Parent() As TvwNode
Public Property Set Parent(ByVal Value As TvwNode)Parent node.
Root
Public Property Get Root() As TvwNodeRoot node.
NextSibling
Public Property Get NextSibling() As TvwNodeNext sibling node.
PreviousSibling
Public Property Get PreviousSibling() As TvwNodePrevious sibling node.
FirstSibling
Public Property Get FirstSibling() As TvwNodeFirst sibling node.
LastSibling
Public Property Get LastSibling() As TvwNodeLast sibling node.
FirstVisibleSibling
Public Property Get FirstVisibleSibling() As TvwNodeFirst visible sibling node.
LastVisibleSibling
Public Property Get LastVisibleSibling() As TvwNodeLast visible sibling node.
NextVisibleSibling
Public Property Get NextVisibleSibling() As TvwNodeNext visible sibling node.
PreviousVisibleSibling
Public Property Get PreviousVisibleSibling() As TvwNodePrevious visible sibling node.
FullPath
Public Property Get FullPath() As StringThe full path from the root to the current node.
Visible
Public Property Get Visible() As BooleanWhether the node is visible.
TvwNode Methods
Move
Public Sub Move(ByVal Relative As Variant, ByVal Relationship As TvwNodeRelationshipConstants)Moves the node to a new position.
EnsureVisible
Public Sub EnsureVisible()Ensures the node is visible (expands parent nodes and scrolls into view).
CreateDragImage
Public Function CreateDragImage() As LongPtrCreates a drag image for the node; returns the image list handle.
SelectedIndex
Public Function SelectedIndex() As LongReturns the selected item index.
TvwNodes Class
Tree view nodes collection.
TvwNodes Members
NewEnum
Public Function NewEnum() As IUnknownEnumerator (hidden).
Add
Public Function Add(Optional ByVal Relative As Variant, Optional ByVal Relationship As Variant, Optional ByVal Key As Variant, Optional ByVal Text As Variant, Optional ByVal Image As Variant, Optional ByVal SelectedImage As Variant) As TvwNodeAdds a node.
Item
Public Function Item(ByVal Index As Variant) As TvwNodeGets a node (default member).
Exists
Public Function Exists(ByVal Index As Variant) As BooleanChecks if a node exists.
Count
Public Property Get Count() As LongNode count.
Clear
Public Sub Clear()Clears all nodes.
Remove
Public Sub Remove(ByVal Index As Variant)Removes a node.
TvwSelectedNodes Class
Selected nodes collection (multi-select mode).
TvwSelectedNodes Members
NewEnum
Public Function NewEnum() As IUnknownEnumerator (hidden).
Item
Public Function Item(ByVal Index As Long) As TvwNodeGets a selected node (default member).
Exists
Public Function Exists(ByVal Index As Long) As BooleanChecks if a selected node exists.
Count
Public Property Get Count() As LongSelected node count.
Code Examples
Basic Usage
' Add tree nodes
Dim root As TvwNode
Set root = TreeView1.Nodes.Add(, , "root", "Root Node", 1)
root.Expanded = True
TreeView1.Nodes.Add "root", TvwNodeChild, "child1", "Child 1", 2
TreeView1.Nodes.Add "root", TvwNodeChild, "child2", "Child 2", 2
' Handle node click
Private Sub TreeView1_NodeClick(ByVal Node As TvwNode, ByVal Button As Integer)
MsgBox "Clicked: " & Node.Text
End Sub
' Control node expansion
Private Sub TreeView1_BeforeExpand(ByVal Node As TvwNode, ByRef Cancel As Boolean)
If Node.Children = 0 Then
Cancel = True
End If
End Sub
' Node selection change
Private Sub TreeView1_NodeSelect(ByVal Node As TvwNode)
Debug.Print "Selected: " & Node.FullPath
End Sub