Get All Text From A TreeView Control Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
EntropicBlackhole
Posts: 40
Joined: 12 Jun 2021, 15:28

Get All Text From A TreeView Control

18 Sep 2021, 17:50

So I've been searching and I havent found a clear way of getting the text from a treeview control, I ended up developing my own function but it only grabs the first generation of children, but not their children
I've had the idea of storing it in an array, an associative one, but I don't think it would work, my function was somewhat inspired on how the ControlGet, List, ListView works

I've also had the idea of storing it like this:

Fruits[Apples[Red, Circular, Hard], Oranges[Orange, Circular, Squishy]]

And it would get that from the TreeView in the image below

Code: Select all

TV_GetTextAll(TreeViewName := "SysTreeView321") {
	Gui, TreeView, %TreeViewName%
	ItemID := 0  ; Causes the loop's first iteration to start the search at the top of the tree.
	Loop
	{
		ItemID := TV_GetNext(ItemID, "Full")  ; Replace "Full" with "Checked" to find all checkmarked items.
		if not ItemID  ; No more items in tree.
			break
		if not TV_GetParent(ItemID)  ; Checks if the item is a parent, if so it stores the text in %ParentText% for later (only works with the top level however).
			TV_GetText(ParentText, ItemID)
		else  ; Checks if the item is a child, if so it stores the text in %ChildText% for later.
		{
			TV_GetText(Child, ItemID)
			ChildText .= Child "|"
			TempItemID := TV_GetNext(ItemID, "Full")
			if not TV_GetParent(TempItemID)
				List .= ParentText "="ChildText "`n"
		}
	}
	return RTrim(List, "`n" "|")
}
I really dont know any other way of doing it, and I've had this problem for a couple of months now, any help would be appreciated!
Attachments
getalltexttv.png
getalltexttv.png (4.31 KiB) Viewed 1424 times
just me
Posts: 9406
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Get All Text From A TreeView Control

19 Sep 2021, 07:05

As a starting point:

Code: Select all

#NoEnv
Gui, Add, TreeView, vTV w300 r10
RID := TV_Add("Fruits")
PID := TV_Add("Apples", RID)
TV_Add("Red", PID)
TV_Add("Circular", PID)
TV_Add("Hard", PID)
PID := TV_Add("Oranges", RID)
TV_Add("Red", PID)
TV_Add("Circular", PID)
TV_Add("Squishy", PID)
Gui, Add, Button, wp gGetAllText, Get All Text
Gui, Show, , TreeView
Return

GuiClose:
ExitApp

GetAllText:
Gui, TreeView, TV ; set the default TreeView (not necessary in this case)
MsgBox, % TV_GetAllText()
Return

TV_GetAllText(ItemID := 0) { ; uses the default TreeView of the default Gui
   Text := ""
   If (ItemID = 0)
      ItemID := TV_GetNext()
   While (ItemID){
      TV_GetText(ItemText, ItemID)
      Text .= ItemText
      If ChildID := TV_GetChild(ItemID)
         Text .= "[" . TV_GetAllText(ChildID) . "],"
      Else
         Text .= ","
      ItemID := TV_GetNext(ItemID)
   }
   Return StrReplace(SubStr(Text, 1, -1), ",]", "]")
}
EntropicBlackhole
Posts: 40
Joined: 12 Jun 2021, 15:28

Re: Get All Text From A TreeView Control

20 Sep 2021, 13:52

mikeyww wrote:
19 Sep 2021, 06:19
This may help. viewtopic.php?f=6&t=36879
Thank you!
EntropicBlackhole
Posts: 40
Joined: 12 Jun 2021, 15:28

Re: Get All Text From A TreeView Control

20 Sep 2021, 13:53

just me wrote:
19 Sep 2021, 07:05
As a starting point:

Code: Select all

TV_GetAllText(ItemID := 0) { ; uses the default TreeView of the default Gui
   Text := ""
   If (ItemID = 0)
      ItemID := TV_GetNext()
   While (ItemID){
      TV_GetText(ItemText, ItemID)
      Text .= ItemText
      If ChildID := TV_GetChild(ItemID)
         Text .= "[" . TV_GetAllText(ChildID) . "],"
      Else
         Text .= ","
      ItemID := TV_GetNext(ItemID)
   }
   Return StrReplace(SubStr(Text, 1, -1), ",]", "]")
}
Thank you so much
EntropicBlackhole
Posts: 40
Joined: 12 Jun 2021, 15:28

Re: Get All Text From A TreeView Control

20 Sep 2021, 16:23

I needed this because im making a treeview list creator
the only thing I need now is to have it disassemble the text given by the function and rearrange it into a code format

Code: Select all

Gui, New, , TreeViewCreator
Gui, Add, Button, w20 h20 gAdd, +
Gui, Add, Button, yp+25 w20 h20 gAddChild, +C
Gui, Add, Button, x+5 ym w40 h20 gDelete, Delete
Gui, Add, Button, yp+25 w40 h20 gModify, Modify
Gui, Add, Button, x+5 ym w40 h45 gDeleteAll, Delete All
Gui, Add, Button, x+5 ym w50 h45 gGen, Generate Code
Gui, Add, Button, x+5 ym w50 h20 gReset, Reset
Gui, Add, Button, yp+25 w50 h20 gRedraw, Redraw
Gui, Add, CheckBox, x+5 ym+1 w40 h20 vBold, Bold
Gui, Add, CheckBox, yp+24 w60 h20 vChecked, Checked ;this is whether the treeview itself will be with checkboxes
Gui, Add, Edit, x+6 ym+1 w70 h42 vName hwndHandle, 
Gui, Add, TreeView, xm W363 h250 -ReadOnly AltSubmit vTV
Gui, Show, , TreeView List Creator
Gui, TreeView, TV
return

Add:
Gui, Submit, NoHide
if Bold
	Options .= "Bold "
if Checked
	Options .= "Check "
TV_Add(Name, , Options "Expand")
Options := ""
GuiControl, , Edit1
GoSub, Redraw
return

AddChild:
Selected := TV_GetSelection()
Gui, Submit, NoHide
if Bold
	Options .= "Bold "
if Checked
	Options .= "Check "
TV_Add(Name, Selected, Options "Expand")
Options := ""
GuiControl, , Edit1
GoSub, Redraw
return

Delete:
Selected := TV_GetSelection()
if (Selected = 0)
	MsgBox, 8208, Sorry, Select an item first., 0
else
	TV_Delete(Selected)
GoSub, Redraw
return

DeleteAll:
MsgBox, 8500, Warning!, Are you sure you want to erase all items?, 0
ifMsgBox, Yes
	TV_Delete()
ifMsgBox, No
	GoSub, Redraw
GoSub, Redraw
return

Modify:
Selected := TV_GetSelection()
if (Selected = 0)
	MsgBox, 8208, Sorry, Select an item first., 0
else
{
	InputBox, Name, New Name, , , 140, 100
	if not ErrorLevel
		TV_Modify(Selected, , Name)
}
GoSub, Redraw
return

Reset:
Reload
return

Redraw:
GuiControl, -Redraw, TV
GuiControl, +Redraw, TV
GuiControl, Focus, Edit1
SendMessage, 0xB1, -2, -1,, ahk_id %Handle%
SendMessage, 0xB7,,,, ahk_id %Handle%
return

Gen:
Gui, Submit, NoHide
GenCode .= "Gui, Add, TreeView, "
GenCode .= Checked = 1 ? "Checked" : ""
GenCode .= "`n"
MsgBox, % TV_GetAllText()
return

TV_GetAllText(ItemID := 0) { ; uses the default TreeView of the default Gui
	Text := ""
	If (ItemID = 0)
		ItemID := TV_GetNext()
	While (ItemID){
		TV_GetText(ItemText, ItemID)
		Text .= ItemText
		If ChildID := TV_GetChild(ItemID)
			Text .= "[" . TV_GetAllText(ChildID) . "],"
		Else
			Text .= ","
		ItemID := TV_GetNext(ItemID)
	}
	Return StrReplace(SubStr(Text, 1, -1), ",]", "]")
}
That's the code for it so far, like I said I just need to disassemble the text into a code format
EntropicBlackhole
Posts: 40
Joined: 12 Jun 2021, 15:28

Re: Get All Text From A TreeView Control

20 Sep 2021, 18:26

just me wrote:
19 Sep 2021, 07:05
As a starting point:

Code: Select all

#NoEnv
Gui, Add, TreeView, vTV w300 r10
RID := TV_Add("Fruits")
PID := TV_Add("Apples", RID)
TV_Add("Red", PID)
TV_Add("Circular", PID)
TV_Add("Hard", PID)
PID := TV_Add("Oranges", RID)
TV_Add("Red", PID)
TV_Add("Circular", PID)
TV_Add("Squishy", PID)
Gui, Add, Button, wp gGetAllText, Get All Text
Gui, Show, , TreeView
Return

GuiClose:
ExitApp

GetAllText:
Gui, TreeView, TV ; set the default TreeView (not necessary in this case)
MsgBox, % TV_GetAllText()
Return

TV_GetAllText(ItemID := 0) { ; uses the default TreeView of the default Gui
   Text := ""
   If (ItemID = 0)
      ItemID := TV_GetNext()
   While (ItemID){
      TV_GetText(ItemText, ItemID)
      Text .= ItemText
      If ChildID := TV_GetChild(ItemID)
         Text .= "[" . TV_GetAllText(ChildID) . "],"
      Else
         Text .= ","
      ItemID := TV_GetNext(ItemID)
   }
   Return StrReplace(SubStr(Text, 1, -1), ",]", "]")
}
question, do you know how I could turn the output text into a code format?
somewhat like this:

Code: Select all

RID := TV_Add("Fruits")
PID := TV_Add("Apples", RID)
TV_Add("Red", PID)
TV_Add("Circular", PID)
TV_Add("Hard", PID)
PID := TV_Add("Oranges", RID)
TV_Add("Orange", PID)
TV_Add("Circular", PID)
TV_Add("Squishy", PID)
just me
Posts: 9406
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Get All Text From A TreeView Control

21 Sep 2021, 04:00

Do you actually need an AHK code format or do you just want to load the TreeView automatically?
EntropicBlackhole
Posts: 40
Joined: 12 Jun 2021, 15:28

Re: Get All Text From A TreeView Control

21 Sep 2021, 12:39

just me wrote:
21 Sep 2021, 04:00
Do you actually need an AHK code format or do you just want to load the TreeView automatically?
No I need the code format because with my script you can easily make treeviews without it taking so long, and I needed it to generate the code when you press Generate Code, so it takes from the treeview, and it now needs it to format this: Fruits[Apples[Red,Circular,Hard],Oranges[Orange,Circular,Squishy]] into a code format, also now that I think of it we could just do it directly without having to take the text out correct? like directly by reading the treeview, we could just format it directly into code without having to pass through the text one
just me
Posts: 9406
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Get All Text From A TreeView Control  Topic is solved

23 Sep 2021, 04:10

I'd prefer a string which can be used to fill a TreeView automatically by a function. But

Code: Select all

#NoEnv
Gui, Add, TreeView, vTV w300 r10
RID := TV_Add("Fruits")
PID := TV_Add("Apples", RID)
TV_Add("Red", PID)
TV_Add("Circular", PID)
TV_Add("Hard", PID)
PID := TV_Add("Oranges", RID)
TV_Add("Red", PID)
TV_Add("Circular", PID)
TV_Add("Squishy", PID)
Gui, Add, Button, wp gGenCode, Generate Code
Gui, Show, , TreeView
Return

GuiClose:
ExitApp

GenCode:
Gui, TreeView, TV ; set the default TreeView (not necessary in this case)
MsgBox, % TV_GetTree()
Return

TV_GetTree(ItemID := 0, Level := 0) { ; uses the default TreeView of the default Gui
   Text := ""
   If (ItemID = 0) {
      ItemID := TV_GetNext()
      Text := "ID0 := 0`r`n"
   }
   While (ItemID){
      TV_GetText(ItemText, ItemID)
      Text .= "ID" . (Level + 1) . " := TV_Add(""" . ItemText . """, ID" . Level . ")`r`n"
      If ChildID := TV_GetChild(ItemID)
         Text .= TV_GetTree(ChildID, Level + 1)
      ItemID := TV_GetNext(ItemID)
   }
   Return (Level = 0 ? RTrim(Text, "`r`n") : Text)
}
EntropicBlackhole
Posts: 40
Joined: 12 Jun 2021, 15:28

Re: Get All Text From A TreeView Control

23 Sep 2021, 11:55

just me wrote:
23 Sep 2021, 04:10
I'd prefer a string which can be used to fill a TreeView automatically by a function. But

Code: Select all

#NoEnv
Gui, Add, TreeView, vTV w300 r10
RID := TV_Add("Fruits")
PID := TV_Add("Apples", RID)
TV_Add("Red", PID)
TV_Add("Circular", PID)
TV_Add("Hard", PID)
PID := TV_Add("Oranges", RID)
TV_Add("Red", PID)
TV_Add("Circular", PID)
TV_Add("Squishy", PID)
Gui, Add, Button, wp gGenCode, Generate Code
Gui, Show, , TreeView
Return

GuiClose:
ExitApp

GenCode:
Gui, TreeView, TV ; set the default TreeView (not necessary in this case)
MsgBox, % TV_GetTree()
Return

TV_GetTree(ItemID := 0, Level := 0) { ; uses the default TreeView of the default Gui
   Text := ""
   If (ItemID = 0) {
      ItemID := TV_GetNext()
      Text := "ID0 := 0`r`n"
   }
   While (ItemID){
      TV_GetText(ItemText, ItemID)
      Text .= "ID" . (Level + 1) . " := TV_Add(""" . ItemText . """, ID" . Level . ")`r`n"
      If ChildID := TV_GetChild(ItemID)
         Text .= TV_GetTree(ChildID, Level + 1)
      ItemID := TV_GetNext(ItemID)
   }
   Return (Level = 0 ? RTrim(Text, "`r`n") : Text)
}
oh my thank you so much, this is exactly what I needed

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 173 guests