Jump to content

Generate Treeview Function


 Share

Recommended Posts

Hi, maybe my question is stupid, but i don't find the answer...

I want to put my treeview creation in a function.

I wrote the following code:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <TreeViewConstants.au3>
#include <StaticConstants.au3>
#Include <GuiTreeView.au3>


GUICreate("AD TreeView", 400, 400)

$treeviewID    = CreateADTree (25, 25, 300, 300, BitOr($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS), $WS_EX_CLIENTEDGE)
$treeview = GUICtrlGetHandle ($treeviewID)

$infobutton     = GUICtrlCreateButton("Info", 25, 350, 70, 20, 0)
$cancelbutton   = GUICtrlCreateButton("Cancel", 125, 350, 70, 20, 0)


GUISetState ()
While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $cancelbutton Or $msg = $GUI_EVENT_CLOSE
            ExitLoop
    
    
        Case $msg = $infobutton
            $item = GUICtrlRead($treeview)   ; Get the controlID of the current selected treeview item
            
            If $item = 0 Then
                MsgBox(64, "TreeView Demo", "No item currently selected")
            Else
                _GUICtrlTreeView_SelectItem($treeview, $item)
            ;MsgBox(4160, "Information", "Parent Handle: " & _GUICtrlTreeView_GetParentHandle($treeview, $item)) 
            ;MsgBox(4160, "Information", "Parent ID: " & _GUICtrlTreeView_GetParentParam($treeview, $item))
                $ParentID = _GUICtrlTreeView_GetParentParam($treeview, $item)
                $ParentText = GUICtrlRead($ParentID, 1)
                
            ;_GUICtrlTreeView_SelectItem($treeview, _GUICtrlTreeView_GetParentHandle($treeview, $item))
                
                $text = GUICtrlRead($item, 1); Get the text of the treeview item
                If $text == "" Then
                    MsgBox(16, "Error", "Error while retrieving infos about item")
                Else
                    MsgBox(64, "TreeView Demo", "Parent Text is : "& $ParentText &" and Current item selected is: " & $text)
                EndIf
            EndIf
            

            
      EndSelect
WEnd

GUIDelete()
Exit


Func CreateADTree ($intLeft, $intTop, $intWidth, $intHeight, $intStyle, $intExStyle )
    Local $LocalTreeview
    
    $LocalTreeview  = GUICtrlCreateTreeView($intLeft, $intTop, $intWidth, $intHeight, $intStyle, $intExStyle)
    
    $hImage = _GUIImageList_Create(16, 16, 5, 3)
    $hFolderImage = _GUIImageList_AddIcon($hImage, "shell32.dll", 4)
    _GUICtrlTreeView_SetNormalImageList($LocalTreeview, $hImage)    
    
    _GUICtrlTreeView_BeginUpdate($LocalTreeview)
    $RootNode = _GUICtrlTreeView_Add($LocalTreeview, 0, "ROOT NODE", $hFolderImage, $hFolderImage)
        _GUICtrlTreeView_AddChild($LocalTreeview, $RootNode, "DO 1",$hFolderImage, $hFolderImage)
        
        $DO2 = _GUICtrlTreeView_AddChild($LocalTreeview, $RootNode, "DO 2", $hFolderImage, $hFolderImage)
            _GUICtrlTreeView_AddChild($LocalTreeview, $DO2, "Sub 1",$hFolderImage,$hFolderImage)
            _GUICtrlTreeView_AddChild($LocalTreeview, $DO2, "Sub 2",$hFolderImage,$hFolderImage)
            _GUICtrlTreeView_AddChild($LocalTreeview, $DO2, "Sub 3",$hFolderImage,$hFolderImage)
            _GUICtrlTreeView_AddChild($LocalTreeview, $DO2, "Sub 4",$hFolderImage,$hFolderImage)
        
        _GUICtrlTreeView_AddChild($LocalTreeview, $RootNode, "DO 3",$hFolderImage, $hFolderImage)
        _GUICtrlTreeView_AddChild($LocalTreeview, $RootNode, "DO 4",$hFolderImage, $hFolderImage)
    
    _GUICtrlTreeView_EndUpdate($LocalTreeview)
    
    Return $LocalTreeview
EndFunc

Well, my treeview is created, but i cannot get the selected item!!

Have you some function to get the texte/name of the selected node ? I'm not sure i'm using the best way...

Thank you very much!!

Link to comment
Share on other sites

_GUICtrlTreeView_GetSelection

This will get you the handle to the current item. You can then use other functions to get the text and other information (like the next sibling or previous sibling, etc.)

Regards,Josh

Link to comment
Share on other sites

_GUICtrlTreeView_GetSelection

This will get you the handle to the current item. You can then use other functions to get the text and other information (like the next sibling or previous sibling, etc.)

Thank you!

I tried with this function before, but i guess i used wrong parameters

anyway, now it works muttley

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...