User Tools

Site Tools


k17:k17.9:k17.9.2:start

17.9.2 Project - Demonstrating the use of the TreeView class

?800

Figure 17.9.2.1: TreeView with item selected.

At appropriate points in the following sections, parts of the source code of the 'Image Viewer' project are given to demonstrate the use of the TreeeView class and briefly commented on:

When starting the programme for the first time, you can use the menu item 'File > Select Base Directory' in a dialogue to select a directory in which images are also stored.This base directory from the system directory structure is mapped with all its sub-directories to a TreeView. Note the recursion in the procedure:

Private Sub DirTree2TreeView(BaseDir As String)
  Dim sDir, sBaseDir, sPattern As String
  Dim iFilter As Integer
 
  sBaseDir = BaseDir
  sPattern = "*"
  iFilter = gb.Directory
 
  If Exist(sBaseDir) Then
     For Each sDir In Dir(sBaseDir, sPattern, iFilter).Sort(gb.Descent)
       TreeView1.Add(sBaseDir &/ sDir, sDir, Picture["icon:/16/directory"], sBaseDir, Null)
       DirTree2TreeView(sBaseDir &/ sDir) ' recursive loop
     Next ' Directory
  Endif ' Exist() ?
 
End

When selecting a directory in the TreeView with a mouse click, a list (array) of all image files in the selected directory is generated:

Public Sub TreeView1_Select()
 
' Bild-Liste für das ausgewählte Verzeichnis in der TreeView erzeugen
  GetImagePaths(TreeView1.Current.Key)
 
  If aImagePaths.Count > 0 Then
     SetEnabled()
     iPictureIndex = 0
     PictureBoxD.Picture = Picture.Load(aImagePaths[0]) ' Anzeige des 1. Bildes in der Bild-Liste
   ' Einschalten der Bild-Navigation, wenn die Bild-Liste mehr als ein Bild enthält
     If aImagePaths.Count > 1 Then
        btnNext.Enabled = True
     Else
        btnNext.Enabled = False
     Endif
  Endif
 
End

A pattern filter (sPattern) ensures that only image files (iFilter = gb.File) with specified extensions (→ JPG, JPEG, jpeg, jpg, png, gif, .svg) are included in the image list. The list contains the (absolute) image file paths.

Private Sub GetImagePaths(sDir As String)
  Dim sFile, sPattern As String
  Dim iFilter As Integer
 
  sPattern = "*{.JPG,.png,.JEPG,.jpeg,.jpg,.gif,.svg}"
  iFilter = gb.File
  aImagePaths = New String[] ' Neue Bild-Liste
  If Exist(sDir) Then
     For Each sFile In Dir(sDir, sPattern, iFilter).Sort(gb.Ascent)
       aImagePaths.Add(sDir &/ sFile)
     Next ' File
  Endif ' Exist() ?
 
End

The first picture in the list is immediately displayed in a picture box. Then you can navigate through the picture list with the two navigation buttons. If the picture list is empty, a default picture is displayed.You can completely expand and collapse the TreeView entries by using the two buttons below the TreeView.

If the number of items in the TreeView is at least one, the internal pointer of the TreeView is set to the first item (lines 1 and 2). Then, in a repeat-until control structure in lines 4 to 6, each parent item is expanded until the internal cursor reaches the bottom TreeView end. However, this causes the function value of the TreeView method trvTreeView.MoveBelow() to become positive (!) and thus the loop to be aborted. Analogous considerations also apply to collapsing:

  [1] Private Sub ExpandAll(trvTreeView As TreeView)
  [2]    If trvTreeView <> Null And trvTreeView.Count > 0 Then
  [3]       trvTreeView.MoveFirst()
  [4]       Repeat
  [5]          trvTreeView.Item.Expanded = True ' → ausklappen
  [6]       Until trvTreeView.MoveBelow()
  [7]    Endif
  [8] End
  [1] Private Sub ContractAll(trvTreeView As TreeView)
  [2]    If trvTreeView <> Null And trvTreeView.Count > 0 Then
  [3]       trvTreeView.MoveFirst()
  [4]       Repeat
  [5]          trvTreeView.Item.Expanded = False ' → einklappen
  [6]       Until trvTreeView.MoveBelow()
  [7]    Endif
  [8] End

Collapsing and expanding the items in the TreeView is only intended to demonstrate selected methods of the TreeView in the project.

If the programme is terminated, then the current directory is entered as the start directory in the settings file dir.conf. Example:

  # Start-Directory
  [Base]
  BasePath="/home/hans/11-Buchprojekt/K17_KomF2/17.9_TreeView/URLAUB"

The default language for the project is English. There is a German language file for the project.

In the project → Chapter 24.3.4 Manager for email accounts you will find another example for the use of a TreeView as navigation in a tree structure.

Note:The project only serves to demonstrate the use of the TreeView class. Therefore, the 'Picture Viewer' programme is still missing some details such as thumbnails or automatic recognition of the picture format that you would have expected for a picture viewer.

Download

The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.
k17/k17.9/k17.9.2/start.txt · Last modified: 01.10.2023 by emma

Page Tools