Get path of selected item

The place for threads about version 1.x of TimoSoft ExplorerTreeView.
Post Reply
Glueckskeks
Cadet
Posts: 2
Joined: 15 Mar 2007, 10:03

Get path of selected item

Post by Glueckskeks » 15 Mar 2007, 10:24

Hi!

I'm using explorer treeview with checkboxes and showing all files. Now I'd like to get the path of a checked file.

Opening folders using the + and activating the checkbox in front of a file, Extvw.path doesn't give me the path of the opened folder but my desktop's. Do I always have to open folders by clicking on it directly? :?

I've got the same problem using Extvw.selecteditem. It gives me an index (well, that's what I believe it is...) but I want to get its filename... or better: its complete path.

It would be nice if you could help me solving this problem.

Glueckskeks :)

User avatar
TiKu
Administrator
Administrator
Posts: 832
Joined: 28 Sep 2004, 21:10
Location: Unterföhring
Contact:

Post by TiKu » 15 Mar 2007, 12:34

Hi,

there's a difference between selected item and checked items. The selected item is the item, that has the keyboard focus. Only one item can be selected. The checked items are those items with a checkmark in front of them. Multiple items can be checked.

The SelectedItem property gives you the selected item's handle (a handle is some sort of index). To get the filename of an item using its handle, you can either use the ItemHandleToDisplayName method or the ItemHandleToFSPath method. ItemHandleToDisplayName gives you the file's name as it is displayed in the treeview. ItemHandleToFSPath gives you the file's path on the harddisk.

To get the filenames of all checked items, you could use the following code:

Code: Select all

Dim hItem As Long

  ' ExplorerTreeView methods that return an item handle, return 0
  ' to identify the root item (only if ShowRoot = False) and -1 to
  ' "identify" a non-existent item.

  hItem = ExTvw1.GetFirstItem     ' can't return 0
  While hItem <> -1
    If ExTvw1.ItemStateIconIndex(hItem) = 2 Then
      ' checked, so write the display name to the debug window
      Debug.Print ExTvw1.ItemHandleToDisplayName(hItem)
    End If

    ' get the next item to check
    hItem = findNextItemToProcess(hItem)     ' won't return 0
  Wend

Private Function findNextItemToProcess(ByVal hBaseItem As Long)
  Dim hDummy As Long
  Dim ret As Long

  ' ExplorerTreeView methods that return an item handle, return 0
  ' to identify the root item (only if ShowRoot = False) and -1 to
  ' "identify" a non-existent item.

  ret = ExTvw1.ItemGetFirstSubItem(hBaseItem)     ' won't return 0
  If ret = -1 Then
    ' the item doesn't have sub-items - try its successor
    ret = ExTvw1.ItemGetNextItem(hBaseItem)     ' won't return 0
    If ret = -1 Then
      ' there is no successor - try the parent item's successor
      ' and if we again don't have success, go upwards until
      ' we find an item with a successor
      ret = hBaseItem
      hDummy = -1
      Do
        ret = ExTvw1.ItemGetParentItem(ret)     ' can return 0
        If (ret <> 0) And (ret <> -1) Then
          hDummy = ExTvw1.ItemGetNextItem(ret)     ' no 0...
        Else
          ' this is a bit rude, but it improves performance,
          ' because we don't have to check <ret> again in
          ' the Loop While statement
          Exit Do
        End If
      Loop While hDummy = -1
      ' at this point hDummy is either a valid handle or -1
      ret = hDummy
    End If
  End If

  findNextItemToProcess = ret
End Function
This code may become slow if the treeview contains many items. To improve performance, you could keep a list of checked items and handle the ItemStateIconChanged event to update this list. The event is fired if an item is checked or unchecked. A state icon index of 1 means unchecked while 2 means checked.

TiKu
Crunching for Fab36_Folding-Division at Folding@Home. Join Fab36/Fab30! - Folding@Home and BOINC
Boycott DRM! Boycott HDCP!

Glueckskeks
Cadet
Posts: 2
Joined: 15 Mar 2007, 10:03

Post by Glueckskeks » 15 Mar 2007, 16:03

Thx a lot!
You really helped me out!

But just now occured some strange thing...
When I check files one after another everything is ok... but as soon as I check a whole folder and want to deselect some of the files in this folder, the folder's checkbox disappears! (not like checkboxes were set =false, but leaving space between tree and folder name)

I believe the problem lies in the semiselect feature.

I'm using the ExTvw_ItemStateIconChanged sub from your TechDemo example:

Code: Select all

Private Sub ExTreeView_ItemStateIconChanged(ByVal hItem As Long, ByVal OldStateIcon As Long, ByVal NewStateIcon As Long)
  Const Deselected = 1
  Const Selected = 2
  Const SemiSelected = 3
  Dim foundDeselected As Boolean
  Dim foundSelected As Boolean
  Dim foundSemiSelected As Boolean
  Dim allSelected As Boolean
  Dim hParent As Long

  setStateIcons hItem, NewStateIcon
    ' Parent-Items updaten
    With ExTreeView
      hParent = .ItemGetParentItem(hItem)
      While (hParent <> -1) And (hParent <> 0)
        foundDeselected = (.ItemGetFirstSubItem(hParent, Deselected) <> -1)
        foundSelected = (.ItemGetFirstSubItem(hParent, Selected) <> -1)
        foundSemiSelected = (.ItemGetFirstSubItem(hParent, SemiSelected) <> -1)

        If foundSemiSelected Then
          .ItemStateIconIndex(hParent) = SemiSelected
        Else
          If foundSelected And foundDeselected Then
            .ItemStateIconIndex(hParent) = SemiSelected
          ElseIf foundSelected And Not foundDeselected Then
            .ItemStateIconIndex(hParent) = Selected
          ElseIf foundDeselected And Not foundSelected Then
            .ItemStateIconIndex(hParent) = Deselected
          End If
        End If

        hParent = .ItemGetParentItem(hParent)
      Wend
    End With

  
End Sub

As soon as my debugger runs through this code:

Code: Select all

If foundSelected And foundDeselected Then
       .ItemStateIconIndex(hParent) = SemiSelected
the folder's checkbox disappears.
SemiSelected is 3. When I use 0, 1 or 2 as value, the checkbox does what it schould do.

In TechDemo my debugger never runs through these lines, so the folder checkbox is checked when I directly check it and unchecked when I just check some files in the folder, but never semiselected. :?

I wonder what's wrong...?

Glueckskeks :)

User avatar
TiKu
Administrator
Administrator
Posts: 832
Joined: 28 Sep 2004, 21:10
Location: Unterföhring
Contact:

Post by TiKu » 15 Mar 2007, 16:10

You need a custom state imagelist (hStateImageList property), which contains an additional icon for the mixed/semiselected state. The default state imagelist contains icons for unchecked and checked state only.
Crunching for Fab36_Folding-Division at Folding@Home. Join Fab36/Fab30! - Folding@Home and BOINC
Boycott DRM! Boycott HDCP!

Post Reply