Binding different controls with similar data types can effectively decrease the time required for coding. This can also help different programmers to face similar problems during their coding and they instantly find the solution to their questions in result. Although the data binding methods and approaches are developed in different frameworks but there still remain some unexpected demands for data bindings such as what we discuss here in the case of ListViews and TreeViews.
Basically TreeViews are widely used wherever user does not have a comprehensive map of the data structure inside the code in mind or when he/she decides to jump over nodes (e.g. drives and folders) by only one click. On the other hand ListViews – in form of what we have in Explorer - are suitable for users who just got used to the data structure. This supports this claim that TreeViews are suitable for data management and exploration but ListViews are better for applying data in program conventions. The first one acts in X/Y direction but the last one acts through Z.
Although the SubItems property of ListViews can improve the extensibility of ListViews, this property is not inherited from ListViewItem so it can not be converted to an identical data type to develop a tree shaped flow of data.
The key to connect these two controls is to save the current TreeNode in the ListView control.
Using the Code
The code is extremely simple. First of all we have to declare a ListView inherited class where we define our custom ListView.
Public Class BindedListView
Now a TreeView object should be declared in the ListView inherited class
Private _TreeView As TreeView
Public Property TreeView() As TreeView
Set(ByVal value As TreeView)
If value IsNot Nothing Then
_TreeView = value
This TreeView is the main reference for our ListView treatments. Obviously, the nodes in the TreeView can not be replaced by TreeView because they have different types so another object should be declared. The _treenode object is the first node in the TreeView that represents the root node of the TreeView.
Private _treenode As TreeNode
The initialize subroutine sets initial values as items.
Private Sub Initialize()
_treenode = _TreeView.Nodes(0)
SmallImageList = _TreeView.ImageList
LargeImageList = _TreeView.ImageList
For i As Integer = 0 To _treenode.Nodes.Count - 1
OnMouseClick event of the ListView has been overridden to determine whether user has clicked a
TreeNode item or the first
item which changes the current path to the upper path.
Protected Overrides Sub OnMouseDoubleClick(ByVal e As System.Windows.Forms.MouseEventArgs)
If SelectedIndices.Count = 1 Then
If SelectedIndices(0) > 0 Then
In GoIn and GoOut subroutines the new
TreeNode is being detected and its
ChildNodes are being casted on the ListView. Finally, the old
_treenode is replaced by new _
treenode and a
PathChanged event rises.
Dim inner As Integer = SelectedIndices(0) - 1
If _treenode.Nodes(inner).Nodes.Count = 0 Then Exit Sub
For i As Integer = 0 To _treenode.Nodes(inner).Nodes.Count - 1
_treenode = _treenode.Nodes(inner)
Catch ex As Exception
If _treenode.Parent.Nodes.Count = 0 Then Exit Sub
For i As Integer = 0 To _treenode.Parent.Nodes.Count - 1
_treenode = _treenode.Parent
Text = _treenode.FullPath
Catch ex As Exception