Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

[function] CreateTreeView


  • Please log in to reply
9 replies to this topic
Learning one
  • Members
  • 1483 posts
  • Last active: Jan 02 2016 02:30 PM
  • Joined: 04 Apr 2009
WARNING: This is old thread. It is continued here.

[function] CreateTreeView
 
Creates TreeView from a string in which each item is placed in new line and hierarchy is defined by Tab character on the left. Item options can be added after one or more Tab characters after item name. This function is public domain.

Function

CreateTreeView(TreeViewDefinitionString) {	; by Learning one
	IDs := {} 
	Loop, parse, TreeViewDefinitionString, `n, `r
	{
		if A_LoopField is space
			continue
		Item := RTrim(A_LoopField, A_Space A_Tab), Item := LTrim(Item, A_Space), Level := 0
		While (SubStr(Item,1,1) = A_Tab)
			Level += 1,	Item := SubStr(Item, 2)
		RegExMatch(Item, "([^`t]*)([`t]*)([^`t]*)", match)	; match1 = ItemName, match3 = Options
		if (Level=0)
			IDs["Level0"] := TV_Add(match1, 0, match3)
		else
			IDs["Level" Level] := TV_Add(match1, IDs["Level" Level-1], match3)
	}
}	; http://www.autohotkey.com/board/topic/92863-function-createtreeview/
Example 1 - basic

TvDefinition=
(
File
	New
	Open
	Save
	Save as...
	Exit
Edit
	Undo
	Cut
	Copy
	Paste
	Delete
View
	Full screen
	Zoom
		100
		200
Help
)

Gui, Add, TreeView, h300
CreateTreeView(TvDefinition)
Gui, Show
return

GuiClose:
ExitApp
Example 2 - items with options

TvDefinition=
(
File				bold
	New
	Open			bold
	Save
	Save as...
	Exit
Edit
	Undo
	Cut
	Copy
	Paste
	Delete
View				Expand
	Full screen		Select
	Zoom
		100
		200
Help
)

Gui, Add, TreeView, h300
CreateTreeView(TvDefinition)
Gui, Show
return

GuiClose:
ExitApp
See also: [function] CreateMenu

My Website • Recommended: AutoHotkey Unicode 32-bit • Join DropBox, Copy


Learning one
  • Members
  • 1483 posts
  • Last active: Jan 02 2016 02:30 PM
  • Joined: 04 Apr 2009

[function] CreateTreeView updated

- now it supports adding TV item options


My Website • Recommended: AutoHotkey Unicode 32-bit • Join DropBox, Copy


tidbit
  • Administrators
  • 2709 posts
  • Hates playing Janitor
  • Last active: Jan 15 2016 11:37 PM
  • Joined: 09 Mar 2008

This is awesome. Treeviews have always been a pain, I have a template made just for them (though, I think I lost it when I got my new computer). This makes it so much more human-readable, thanks!


rawr. be very afraid
*poke*
. Populate the AutoHotkey city. Pointless but somewhat fun. .


Coco
  • Members
  • 697 posts
  • Last active: Oct 31 2015 07:26 PM
  • Joined: 27 Jul 2012

Cool! This makes it easier, thanks! I have a collection of functions that uses XML to create TreeView(s) with support for moving, indenting, etc. , still unfinished though..



rbrtryn
  • Members
  • 1177 posts
  • Last active: Sep 11 2013 08:04 PM
  • Joined: 22 Jun 2011

+100

 

Adding it to my standard library grin.png

 

[nitpick]

You could replace your RegExReplace with:

RegExMatch(Item,  "([^`t]*)([`t]*)([^`t]*)", match)

Has the same effect and is more compact.

[/nitpick]


My Scripts are written for the latest released version of AutoHotkey.

Need a secure, accessible place to backup your stuff? Use Dropbox!


Pulover
  • Members
  • 1596 posts
  • Last active: Apr 06 2016 04:00 AM
  • Joined: 20 Apr 2012
Great work! Thanks!

Rodolfo U. Batista
Pulover's Macro Creator - Automation Tool (Recorder & Script Writer) | Class_LV_Rows - Copy, Cut, Paste and Drag ListViews | Class_Toolbar - Create and modify | Class_Rebar - Adjustable GUI controls

Join the New AutoHotkey Forum!


Joy2DWorld
  • Members
  • 562 posts
  • Last active: Jun 30 2014 07:48 PM
  • Joined: 04 Dec 2006

        While (SubStr(Item,1,1) = A_Tab)
            Level += 1,    Item := SubStr(Item, 2)

 

 

 

Looping is one way of counting characters in a string,

 

but other ways more direct,

 

ex., regexmatch(item,"$(\t+)",_),  item := substr(item, level := strlen(_1))

 

could also just regexreplace away the tabs in second var & compare length to set level, etc.

 

hope sugestive.


Joyce Jamce

Learning one
  • Members
  • 1483 posts
  • Last active: Jan 02 2016 02:30 PM
  • Joined: 04 Apr 2009

Thanks for suggestions and compliments! wink.png
@rbrtryn: RegExMatch is made more compact now. Thanks!
@Joy2DWorld: Hey, nice to see you again on AHK forum. I appreciate your suggestion, but I'll leave While loop.
 
* * *
 
Related: I totally rewrote and upgraded CreateMenu function, which uses the same logic to create menus. Will be released soon.


My Website • Recommended: AutoHotkey Unicode 32-bit • Join DropBox, Copy


tps242
  • Members
  • 26 posts
  • Last active: Jan 21 2015 06:50 AM
  • Joined: 01 Dec 2014

Does gLabels not work with this?

 

TvDefinition=
(
Game Client bold Expand
Client
Launch/Relaunch gLaunch
Log Off 
Quit
)
 

Gui, Add, TreeView, h300
CreateTreeView(TvDefinition)
 
Gui, Show
return
 
GuiClose:
ExitApp
 

CreateTreeView(TreeViewDefinitionString) { ; by Learning one
IDs := {} 
Loop, parse, TreeViewDefinitionString, `n, `r
{
if A_LoopField is space
continue
Item := RTrim(A_LoopField, A_Space A_Tab), Item := LTrim(Item, A_Space), Level := 0
While (SubStr(Item,1,1) = A_Tab)
Level += 1, Item := SubStr(Item, 2)
RegExMatch(Item, "([^`t]*)([`t]*)([^`t]*)", match) ; match1 = ItemName, match3 = Options
if (Level=0)
IDs["Level0"] := TV_Add(match1, 0, match3)
else
IDs["Level" Level] := TV_Add(match1, IDs["Level" Level-1], match3)
}
} ; http://www.autohotkey.com/board/topic/92863-function-createtreeview/
 
Shift::Reload
 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; GAME CLIENT - CLIENT ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
Launch:
IfWinNotActive, MapleStory, , WinActivate, MapleStory,
IfWinExist,MapleStory
{
WinClose
} else {
SplashImage,,M,,Maplestory is being relaunched/launched, Notice, 
Run C:\Nexon\MapleStory\HawtMaple.exe
Sleep, 5000
ControlSetText,Static1,If this window persists after Maplestory has launched; press {~}, Notice
SetTimer, MapleExist, 65
}
Return
 
MapleExist:
IfWinExist, MapleStory
{
SplashImage, Off
SetTimer, MapleExist, Off
}
return
 
splashreturn:
WinMove, Screenshot, , 0,  A_ScreenHeight-120
return
 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 


tps242
  • Members
  • 26 posts
  • Last active: Jan 21 2015 06:50 AM
  • Joined: 01 Dec 2014

Okay I added it to the beginning and it works but adding AltSubmit next to the row and putting (A_GuiEvent == "Normal") under Launcher: label is not doing anything to stop it from launching, and only when the user left clicks it

 

 

Labels are completely ignored if i put it before showgui and it just continues line after line..until it hits a return

 

If I put it after showgui, nothing after the code applies the return, wth.