Sign in to follow this  
Followers 0
AaronOoi

Get TreeViewItem from child to parent text

4 posts in this topic

#1 ·  Posted (edited)

Hi,

I tried to capture an expanded Tree View item text from its child to it's parent.

I already tried my best to make it with my limited logic knowledge :)

When i clicked on OK button, the expected results should be Information Technology > AP > India > Workstations

img.jpg

#NoTrayIcon

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <TreeViewConstants.au3>
#include <WindowsConstants.au3>

$Form1_1 = GUICreate("Form1", 394, 300, 193, 124)
$TreeView1 = GUICtrlCreateTreeView(8, 20, 369, 200)

$TreeView1_83 = GUICtrlCreateTreeViewItem("Information Technology", $TreeView1)
$TreeView1_84 = GUICtrlCreateTreeViewItem("AP", $TreeView1_83)

$TreeView1_93 = GUICtrlCreateTreeViewItem("Malaysia", $TreeView1_84)
$TreeView1_94 = GUICtrlCreateTreeViewItem("Workstations", $TreeView1_93)
$TreeView1_95 = GUICtrlCreateTreeViewItem("Workstations-Test", $TreeView1_93)
$TreeView1_96 = GUICtrlCreateTreeViewItem("Imaging-Workstations", $TreeView1_95)
$TreeView1_97 = GUICtrlCreateTreeViewItem("India", $TreeView1_84)
$TreeView1_98 = GUICtrlCreateTreeViewItem("Workstations", $TreeView1_97)

$Button1 = GUICtrlCreateButton("Close", 288, 250, 81, 25)
$Button2 = GUICtrlCreateButton("Ok", 192, 250, 81, 25)
GUISetState(@SW_SHOW)


While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            Exit
        Case $Button2
            $idItem = GUICtrlRead($Treeview1)
            if StringInStr(GUICtrlRead($Treeview1,1),"workstation") = 0 then
                msgbox(16,$idItem,"Only workstations allowed to select.")
            Else
                msgbox(0,$idItem,GUICtrlRead($idItem-3,1)& " > " & GUICtrlRead($idItem-2,1)& " > " & GUICtrlRead($idItem-1,1)& " > " & GUICtrlRead($idItem,1))
            EndIf
    EndSwitch
WEnd
Edited by AaronOoi

Share this post


Link to post
Share on other sites



AaronOoi,

Here's one way to fo it...

#NoTrayIcon

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <TreeViewConstants.au3>
#include <WindowsConstants.au3>
 #include <GuiTreeView.au3>

$Form1_1 = GUICreate("Form1", 394, 300, 193, 124)
$TreeView1 = GUICtrlCreateTreeView(8, 20, 369, 200)

$TreeView1_83 = GUICtrlCreateTreeViewItem("Information Technology", $TreeView1)
$TreeView1_84 = GUICtrlCreateTreeViewItem("AP", $TreeView1_83)

$TreeView1_93 = GUICtrlCreateTreeViewItem("Malaysia", $TreeView1_84)
$TreeView1_94 = GUICtrlCreateTreeViewItem("Workstations", $TreeView1_93)
$TreeView1_95 = GUICtrlCreateTreeViewItem("Workstations-Test", $TreeView1_93)
$TreeView1_96 = GUICtrlCreateTreeViewItem("Imaging-Workstations", $TreeView1_95)
$TreeView1_97 = GUICtrlCreateTreeViewItem("India", $TreeView1_84)
$TreeView1_98 = GUICtrlCreateTreeViewItem("Workstations", $TreeView1_97)

$Button1 = GUICtrlCreateButton("Close", 288, 250, 81, 25)
$Button2 = GUICtrlCreateButton("Ok", 192, 250, 81, 25)
GUISetState(@SW_SHOW)

local $iCurrItem, $sOut = ''

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            Exit
        Case $Button2
            $idItem = GUICtrlRead($Treeview1)
            if StringInStr(GUICtrlRead($Treeview1,1),"workstation") = 0 then
                msgbox(16,$idItem,"Only workstations allowed to select.")
            Else
                $iCurrItem = _GUICtrlTreeView_GetSelection($TreeView1)
                while _guictrltreeview_getparenthandle($TreeView1,$iCurrItem) <> 0
                    $sOut = _guictrltreeview_gettext($TreeView1,$iCurrItem) & ' > ' & $sOut
                    $iCurrItem =  _guictrltreeview_getparenthandle($TreeView1,$iCurrItem)
                wend
                $sOut = _guictrltreeview_gettext($TreeView1,$iCurrItem) & ' > ' & $sOut
                $sOut = stringtrimright($sOut,3)

                ConsoleWrite($sOut & @CRLF)

            EndIf
    EndSwitch
WEnd

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

FUBAR,

Amazing, this is exactly what I need, I could have not know that is the right way to do it like years!!

Thank You

Share this post


Link to post
Share on other sites

Aaron,

Forgot to reset var.  Results are wrong when you click multiple workstations...

#NoTrayIcon

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <TreeViewConstants.au3>
#include <WindowsConstants.au3>
 #include <GuiTreeView.au3>

$Form1_1 = GUICreate("Form1", 394, 300, 193, 124)
$TreeView1 = GUICtrlCreateTreeView(8, 20, 369, 200)

$TreeView1_83 = GUICtrlCreateTreeViewItem("Information Technology", $TreeView1)
$TreeView1_84 = GUICtrlCreateTreeViewItem("AP", $TreeView1_83)

$TreeView1_93 = GUICtrlCreateTreeViewItem("Malaysia", $TreeView1_84)
$TreeView1_94 = GUICtrlCreateTreeViewItem("Workstations", $TreeView1_93)
$TreeView1_95 = GUICtrlCreateTreeViewItem("Workstations-Test", $TreeView1_93)
$TreeView1_96 = GUICtrlCreateTreeViewItem("Imaging-Workstations", $TreeView1_95)
$TreeView1_97 = GUICtrlCreateTreeViewItem("India", $TreeView1_84)
$TreeView1_98 = GUICtrlCreateTreeViewItem("Workstations", $TreeView1_97)

$Button1 = GUICtrlCreateButton("Close", 288, 250, 81, 25)
$Button2 = GUICtrlCreateButton("Ok", 192, 250, 81, 25)
GUISetState(@SW_SHOW)

local $iCurrItem, $sOut = ''

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            Exit
        Case $Button2
            $idItem = GUICtrlRead($Treeview1)
            if StringInStr(GUICtrlRead($Treeview1,1),"workstation") = 0 then
                msgbox(16,$idItem,"Only workstations allowed to select.")
            Else
                $iCurrItem = _GUICtrlTreeView_GetSelection($TreeView1)
                while _guictrltreeview_getparenthandle($TreeView1,$iCurrItem) <> 0
                    $sOut = _guictrltreeview_gettext($TreeView1,$iCurrItem) & ' > ' & $sOut
                    $iCurrItem =  _guictrltreeview_getparenthandle($TreeView1,$iCurrItem)
                wend
                $sOut = _guictrltreeview_gettext($TreeView1,$iCurrItem) & ' > ' & $sOut
                $sOut = stringtrimright($sOut,3)

                ConsoleWrite($sOut & @CRLF)

                $sOut = ''  ; <<<<<-------  forgot to blank out var for successive selections during single instance of script

            EndIf
    EndSwitch
WEnd

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0