A few question for a TreeView

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
WOlfen
Posts: 61
Joined: 14 Jan 2018, 16:48

A few question for a TreeView

26 Nov 2020, 19:17

Hello.
Okay, so i worked a little bit on a TreeView and i really like it so far. It does what i need.
So far i got this:

Code: Select all

Gui, SetupMenu: Default
Gui, SetupMenu: Add, TreeView, x50 %Distance1% w480 h270 cff9e03 gFileSelect +0x1000 -HScroll
AddSubFoldersToTree("Z:\TEST\FOLDER1")

AddSubFoldersToTree(Folder, ParentItemID = 0)
{
  Loop, Files, %Folder%\*.*, D F
  if InStr(FileExist(A_LoopFileFullPath), "D")
  AddSubFoldersToTree(A_LoopFileFullPath, TV_Add(A_LoopFileName, ParentItemID, "Icon1"))
  else
  AddSubFoldersToTree(A_LoopFileFullPath, TV_Add(A_LoopFileName, ParentItemID, "Icon2"))
}

FileSelect:
if (A_GuiEvent = "DoubleClick")
{
    TV_GetText(RowText, A_EventInfo)
    ToolTip You double-clicked "%RowText%"
    if (RowText = "*.exe")||RowText = "*.bat")
    {
        ToolTip You double-clicked a Program!   ;                   This doesn´t work!
    }
}
Return
Now i have 3 questions about it.
- As you can see, i want that the script notice if i click on a "exe" or a "bat". When i click on one it should give me a different Tooltip. But it´snot
working so far.
- Right now the Script gives me a Tooltip for every item i click in this List, but i would like to disable having a Tooltip when clicking on a Folder only.
- And lastly i would like to know how i sort this File & Folder Hierarchy inside the TreeView like it is in Windows Explorer. So first the Folders and
then the Files in alphabetical order. I read through the help, but now i´m more confused.
Thanks for any help. :)
User avatar
mikeyww
Posts: 27070
Joined: 09 Sep 2014, 18:38

Re: A few question for a TreeView

26 Nov 2020, 19:45

This is a bit confusing. Is it your intent that the entire script is an auto-execute section?

Have you tested what the TV_GetText is actually returning, before you try to compare its strings to something else?

The "If RowText" line has an extra parenthesis.

I think I would start with a smaller version of this script, and get the basic treeview working and visible first. You can then add the other routines, one at a time, and get each part working.
just me
Posts: 9482
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: A few question for a TreeView

27 Nov 2020, 05:36

Code: Select all

if (RowText = "*.exe")||RowText = "*.bat")
You cannot use something like wildcards here.

Code: Select all

If (SubStr(RowText, -3) = ".exe") Or (SubStr(RowText, -3) = ".bat")
; or
If (RowText ~= "\.(exe|bat)$") ; shorthand for RegExMatch()
User avatar
mikeyww
Posts: 27070
Joined: 09 Sep 2014, 18:38

Re: A few question for a TreeView

27 Nov 2020, 06:31

An example of sorting is below.

Code: Select all

dir = Z:\TEST\FOLDER1
Gui, Add, TreeView, w300 r20 gFileSelect
AddSubFolderToTree(dir)
Gui, Show, Center, File List TreeView
Return

FileSelect:
If (A_GuiEvent != "DoubleClick")
 Return
TV_GetText(RowText, A_EventInfo)
ToolTip You double-clicked "%RowText%"
If (RowText ~= "\.(exe|bat)$")
 ToolTip You double-clicked a Program!
Return

GuiEscape:
GuiClose:
ExitApp

AddSubFolderToTree(Folder, ParentItemID := 0) {
 ; https://autohotkey.com/board/topic/39168-populating-a-treeview-with-folders-and-files/
 Loop, Files, %Folder%\*.*, DF
  ID := TV_Add(A_LoopFileName, ParentItemID, Sort), AddSubFolderToTree(A_LoopFileFullPath, ID)
}
WOlfen
Posts: 61
Joined: 14 Jan 2018, 16:48

Re: A few question for a TreeView

27 Nov 2020, 07:03

Thanks to both of you. :)
just me wrote:
27 Nov 2020, 05:36

Code: Select all

If (SubStr(RowText, -3) = ".exe") Or (SubStr(RowText, -3) = ".bat")
; or
If (RowText ~= "\.(exe|bat)$") ; shorthand for RegExMatch()
Thanks for the help.
Now, is it possible to add a third option that when clicking on a Folder, it will give me a different tooltip?
Something like:

Code: Select all

FileSelect:
if (A_GuiEvent = "DoubleClick")
{
TV_GetText(RowText, A_EventInfo)
ToolTip You double-clicked "%RowText%"
if (RowText = "*.exe")||RowText = "*.bat")
{
ToolTip You double-clicked a Program!
}
if (RowText = "FOLDER") ; i don´t know how it recognizes it as an folder
{
ToolTip You double-clicked a Folder!
}
}
Return
mikeyww wrote:
27 Nov 2020, 06:31
An example of sorting is below.

Code: Select all

AddSubFolderToTree(Folder, ParentItemID := 0) {
 ; https://autohotkey.com/board/topic/39168-populating-a-treeview-with-folders-and-files/
 Loop, Files, %Folder%\*.*, DF
  ID := TV_Add(A_LoopFileName, ParentItemID, Sort), AddSubFolderToTree(A_LoopFileFullPath, ID)
}
Thanks for the example. I found the original code for this somewhere on this forum, i can´t remember.
So it looks like that last part of your example seems to be a shorter Version of it?
Well, i don´t know if i correctly using it, i expected that it now shows the folder first and then the files. But it still behaves like before that it just shows all Files and Folders in alphabetical order. Am i missing something?
Thank you.
User avatar
mikeyww
Posts: 27070
Joined: 09 Sep 2014, 18:38

Re: A few question for a TreeView

27 Nov 2020, 08:08

Yes, it's just a shorter version for the example of using Return at the top, sorting, and integrating the file extension check.

This may help.

Code: Select all

dir = Z:\TEST\FOLDER1
Gui, Add, TreeView, w300 r20 gFileSelect
AddSubFolderToTree(dir)
Gui, Show, Center, Tree
Return

FileSelect:
If (A_GuiEvent != "DoubleClick")
 Return
TV_GetText(RowText, A_EventInfo), msg := "You double-clicked """ RowText """."
msg := TV_GetChild(A_EventInfo) ? "This item has children." : msg
msg := RowText ~= "\.(exe|bat)$" ? "This item is a program." : msg
ToolTip, %msg%
Return

GuiEscape:
GuiClose:
ExitApp

AddSubFolderToTree(Folder, ParentItemID := 0) {
 ; https://autohotkey.com/board/topic/39168-populating-a-treeview-with-folders-and-files/
 Static section := "DF"
 Loop, Parse, section
 {
  thisSection := A_LoopField
  Loop, Files, %Folder%\*.*, %thisSection%
  {
   ID := TV_Add(A_LoopFileName, ParentItemID, thisSection = "D" ? "Bold" : "")
   AddSubFolderToTree(A_LoopFileFullPath, ID)
  }
 }
}
WOlfen
Posts: 61
Joined: 14 Jan 2018, 16:48

Re: A few question for a TreeView

28 Nov 2020, 05:56

mikeyww wrote:
27 Nov 2020, 08:08
Yes, it's just a shorter version for the example of using Return at the top, sorting, and integrating the file extension check.

This may help.

Code: Select all

dir = Z:\TEST\FOLDER1
Gui, Add, TreeView, w300 r20 gFileSelect
AddSubFolderToTree(dir)
Gui, Show, Center, Tree
Return

FileSelect:
If (A_GuiEvent != "DoubleClick")
 Return
TV_GetText(RowText, A_EventInfo), msg := "You double-clicked """ RowText """."
msg := TV_GetChild(A_EventInfo) ? "This item has children." : msg
msg := RowText ~= "\.(exe|bat)$" ? "This item is a program." : msg
ToolTip, %msg%
Return

GuiEscape:
GuiClose:
ExitApp

AddSubFolderToTree(Folder, ParentItemID := 0) {
 ; https://autohotkey.com/board/topic/39168-populating-a-treeview-with-folders-and-files/
 Static section := "DF"
 Loop, Parse, section
 {
  thisSection := A_LoopField
  Loop, Files, %Folder%\*.*, %thisSection%
  {
   ID := TV_Add(A_LoopFileName, ParentItemID, thisSection = "D" ? "Bold" : "")
   AddSubFolderToTree(A_LoopFileFullPath, ID)
  }
 }
}
mikey, thank you so much for your help.
Not only does my script now showing the Folder first and then the Files, but also in this nice bold Style, which i actually prefer. Thank you for this.
Now, a last thing.
I heard a lot of things that it´s possible to get icons in a TreeView. I read through the help file, but i just can´t make it out.
All i want to do is having the same icons that each file uses like in Windows Explorer, showing up in the TreeView here too. Folders of course have also their Folder Icon. Is this even possible?
Thank you for your help. :)
User avatar
mikeyww
Posts: 27070
Joined: 09 Sep 2014, 18:38

Re: A few question for a TreeView

28 Nov 2020, 07:25

It is possible. Adding a static folder icon is easy with the icon option for TV_Add. Since AHK uses an image list for the icons, adding file icons would probably entail the following.

1. Identify the files.
2. Create the image list, keeping track of which icon matches each file. This could be done via an array, adding the icons in the same order as the files, or using the treeview ID in conjunction with the image-list ID or icon number.
3. Add the files to the treeview, specifying the right icon number for each file. Alternatively, you could update the icons after adding the files.

I leave that part to you!
WOlfen
Posts: 61
Joined: 14 Jan 2018, 16:48

Re: A few question for a TreeView

02 Dec 2020, 07:35

@mikeyww

if you don´t mind, i have another question.
I noticed that i somehow can´t get a variable out of the script anymore.
This is how it´s currently build:

Code: Select all

Gui, SetupMenu: Default
Gui, SetupMenu: Add, TreeView, x50 %Distance1% w480 h270 cff9e03 vProgramSelect gFileSelect +0x1000 -HScroll

AddSubFoldersToTree("" Path_App "")

AddSubFoldersToTree(Folder, ParentItemID := 0)
{
	Static section := "DF"
	Loop, Parse, section
	{
		thisSection := A_LoopField
		Loop, Files, %Folder%\*.*, %thisSection%
		{
			ID := TV_Add(A_LoopFileName, ParentItemID, thisSection = "D" ? "Bold" : "")
			AddSubFoldersToTree(A_LoopFileFullPath, ID)
		}
	}
}

FileSelect:
Gui, SetupMenu: Submit, NoHide
{
	if A_GuiControl != ProgramSelect
	Return
	if (A_GuiEvent = "DoubleClick")
	{
		TV_GetText(RowText, A_EventInfo)
		If (SubStr(RowText, -3) = ".exe") || (SubStr(RowText, -3) = ".bat")
		{
			SplitPath, RowText, ProcessName, DirTest, ExtensionTest, NameNoExtTest, DriveTest
			SplitPath, ProcessName, , FullPath
			ToolTip You double-clicked %DirTest%
		}
	}
}
Return
[Mod edit: c-tags replaced with [code][/code] tags.]
But for some unknown reason, the ToolTip gives me nothing. Is it because the "TV_GetText" literally only get´s the Text and not the File, which then would explain why it´s not getting any Path from it. But why does for example "ExtensionTest" work if it would only getting Text?
Basically what i need is the Path of which the doubleclicked file from the TreeView lies.
User avatar
mikeyww
Posts: 27070
Joined: 09 Sep 2014, 18:38

Re: A few question for a TreeView

02 Dec 2020, 07:40

Retrieving the text retrieves the text of what you see, not the whole path, because you did not save the whole path. The following recent posts may help with that issue. Using a loop, building the path is easy if you have the whole tree.

https://www.autohotkey.com/boards/viewtopic.php?f=76&t=83876

https://www.autohotkey.com/boards/viewtopic.php?f=76&t=83866
hd0202
Posts: 183
Joined: 04 Oct 2013, 03:07
Location: Germany near Cologne

Re: A few question for a TreeView

02 Dec 2020, 08:59

store the full path or the directory in a second, hidden column
Hubert
WOlfen
Posts: 61
Joined: 14 Jan 2018, 16:48

Re: A few question for a TreeView

02 Dec 2020, 10:07

mikeyww wrote:
02 Dec 2020, 07:40
Retrieving the text retrieves the text of what you see, not the whole path, because you did not save the whole path. The following recent posts may help with that issue. Using a loop, building the path is easy if you have the whole tree.

https://www.autohotkey.com/boards/viewtopic.php?f=76&t=83876

https://www.autohotkey.com/boards/viewtopic.php?f=76&t=83866
Thanks for your quick response.
I took a look at these threads you mentioned and managed to insert some of the script from the example you gave in the first thread.
Although i must be blind, i can´t get it working that it gives me the Tooltip when i only double click on items. Right now it also responds if i just single click on a item.
What´s wrong here?

Code: Select all

FileSelect:
Gui, SetupMenu: Submit, NoHide
{
	if (A_GuiEvent = "DoubleClick")
	{
		TV_GetText(SelectedItemText, A_EventInfo)
		ParentID := A_EventInfo
		Loop
		{
			ParentID := TV_GetParent(ParentID)
			if not ParentID
			break
			TV_GetText(ParentText, ParentID)
			SelectedItemText := ParentText "\" SelectedItemText
		}
	}
ToolTip You double-clicked %SelectedItemText%
}
Return
[Mod edit: c-tags replaced with [code][/code] tags.]
User avatar
mikeyww
Posts: 27070
Joined: 09 Sep 2014, 18:38

Re: A few question for a TreeView

02 Dec 2020, 10:38

Put the ToolTip command inside the If statement that confirms the double-click.

As far as I know, the tip remains active until you turn it off or exit the script.

The "second hidden column" idea is also cool. I have not tried that.
WOlfen
Posts: 61
Joined: 14 Jan 2018, 16:48

Re: A few question for a TreeView

03 Dec 2020, 08:29

mikeyww wrote:
02 Dec 2020, 10:38
Put the ToolTip command inside the If statement that confirms the double-click.

As far as I know, the tip remains active until you turn it off or exit the script.

The "second hidden column" idea is also cool. I have not tried that.
Thanks again for your response.
Once again, i´m feeling dumb. Everytime i ask you something, you give me a good direction, i managed to correct it in my script, and then something new approaches me in my script. And i don´t anymore how i did that too because i got that running succesfully too before.
Anyway, what i´m trying to achieve is this:
Folder Structure is like this:

Code: Select all

C:\AutoHotKey\APP_FOLDER\Test Folder 1\SubFolder\UnknownName1.exe
C:\AutoHotKey\APP_FOLDER\Test Folder 2\UnknownName2.exe
With your example from before i can get the parent Folder, which for "UnknownName1.exe" would be "SubFolder". This works great.
However, in addition i would also need the relative Path without the exe, without the APP_FOLDER and what comes before it. So it would look like this (if i choose an exe in one of these 2 Paths):

Code: Select all

Test Folder 1\SubFolder
Test Folder 2
User avatar
mikeyww
Posts: 27070
Joined: 09 Sep 2014, 18:38

Re: A few question for a TreeView

03 Dec 2020, 08:53

Does it work with the entire path? That is a good first step.
WOlfen
Posts: 61
Joined: 14 Jan 2018, 16:48

Re: A few question for a TreeView

03 Dec 2020, 08:59

mikeyww wrote:
03 Dec 2020, 08:53
Does it work with the entire path? That is a good first step.
I´m pretty sure i can work with the Full Path, but first i have to get there. Looking at the threads right now again, but can´t point my finger on it what the solution is to be honest. :)
just me
Posts: 9482
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: A few question for a TreeView

03 Dec 2020, 09:05

As you like:

Code: Select all

Root := "Z:\TEST\FOLDER1"
; ...
AddSubFolderToTree(Root)
; ...
If (A_GuiEvent = "DoubleClick") {
   TV_GetText(FileOrFolder, A_EventInfo)
   RelativePath := ""
   ParentID := A_EventInfo
   While (ParentID := TV_GetParent(ParentID)) {
      TV_GetText(ParentText, ParentID)
      RelativePath := ParentText . "\" . RelativePath
   }
   FullPath := Root . "\" . Relativepath . FileOrFolder
   ToolTip You double-clicked`nFileOrFolder: %FileOrFolder%`nRelative Path: %RelativePath%`nFull Path: %FullPath%
}
*not tested*
WOlfen
Posts: 61
Joined: 14 Jan 2018, 16:48

Re: A few question for a TreeView

03 Dec 2020, 09:37

just me wrote:
03 Dec 2020, 09:05
As you like:

Code: Select all

Root := "Z:\TEST\FOLDER1"
; ...
AddSubFolderToTree(Root)
; ...
If (A_GuiEvent = "DoubleClick") {
   TV_GetText(FileOrFolder, A_EventInfo)
   RelativePath := ""
   ParentID := A_EventInfo
   While (ParentID := TV_GetParent(ParentID)) {
      TV_GetText(ParentText, ParentID)
      RelativePath := ParentText . "\" . RelativePath
   }
   FullPath := Root . "\" . Relativepath . FileOrFolder
   ToolTip You double-clicked`nFileOrFolder: %FileOrFolder%`nRelative Path: %RelativePath%`nFull Path: %FullPath%
}
*not tested*
Oh, wow, that works as expected. Thank you.
One tiny, tiny thing thou. I notice when i have this Folder Structure:

Code: Select all

Z:\TEST\APP_FOLDER\Test Folder\UnknownName1.exe
that "RelativePath" gives me "Test Folder\" and "FullPath" gives me "\Test Folder\UnknownName1.exe".
I would need that that "RelativePath" would give me "Test Folder" and "FullPath" would give me "Test Folder\UnknownName1.exe", basically without the "\" at the beginning.
I know i could change that with SubStr the Variable, but i´m not sure how it would affect any paths that are deeper then the example above.
just me
Posts: 9482
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: A few question for a TreeView

03 Dec 2020, 11:47

Code: Select all

If (A_GuiEvent = "DoubleClick") {
   TV_GetText(FileOrFolder, A_EventInfo)
   RelativePath := ""
   ParentID := A_EventInfo
   While (ParentID := TV_GetParent(ParentID)) {
      TV_GetText(ParentText, ParentID)
      RelativePath := ParentText . "\" . RelativePath
   }
   FullPath := Relativepath . FileOrFolder
   RelativPath := RTrim(RelativPath, "\")
   ToolTip You double-clicked`nFileOrFolder: %FileOrFolder%`nRelative Path: %RelativePath%`nFull Path: %FullPath%
}
?
garry
Posts: 3772
Joined: 22 Dec 2013, 12:50

Re: A few question for a TreeView

07 Dec 2020, 09:38

I just tried the last example from user 'just me' to get the fullpath , seems OK

Code: Select all

;-------- saved at 星期一 十二月 2020-12-07  14:11 UTC --------------
;- A few question for a TreeView 
;- https://www.autohotkey.com/boards/viewtopic.php?f=76&t=83713

dir = C:\TEST
Gui, Add, TreeView, w300 r20 gFileSelect
AddSubFolderToTree(dir)
Gui, Show, Center, File List TreeView
Return
;-------------------
GuiClose:
ExitApp
;-------------------
FileSelect:
Gui,1:submit,nohide
If (A_GuiEvent != "DoubleClick")
 Return
If (A_GuiEvent = "DoubleClick") {
   TV_GetText(FileOrFolder, A_EventInfo)
   RelativePath := ""
   ParentID := A_EventInfo
   While (ParentID := TV_GetParent(ParentID)) {
      TV_GetText(ParentText, ParentID)
      RelativePath := ParentText . "\" . RelativePath
   }
   FullPath := dir . "\" . Relativepath . FileOrFolder
   RelativPath := RTrim(RelativPath, "\")
   msgbox, 262208, ,You double-clicked`nFileOrFolder: %FileOrFolder%`nRelative Path: %RelativePath%`nFull Path: %FullPath%
   ;run,%fullpath%
}
Return
;---------------
AddSubFolderToTree(Folder, ParentItemID := 0) {
 ; https://autohotkey.com/board/topic/39168-populating-a-treeview-with-folders-and-files/
 Loop, Files, %Folder%\*.*, DF
  ID := TV_Add(A_LoopFileName, ParentItemID, Sort), AddSubFolderToTree(A_LoopFileFullPath, ID)
}
;========= END SCRIPT TEST-TreeView =============================================

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 138 guests