Jump to content

Tree View reads Empty


Recommended Posts

I have code to read when the selected item in a tree view changes. I use the selected item's text to perform a search later, but this is a stripped down version. About 1/3 of the time it won't read the selected item's text when I run the script. The rest of the time everything goes great.

#include <GUIConstants.au3>
#include <GuiTreeView.au3>

Global $idFileTreeView

Main()

Func Main()

    GUICreate("Function Docs", 300, 375)
    $idFileTreeView = GUICtrlCreateTreeView(10, 10, 250, 350)

    GUISetState()

    LoadTreeView()

    Local $iTemp, $iSelected, $sTreePath, $sText

    While True
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
            Case Else
                $iTemp = _GUICtrlTreeView_GetSelection($idFileTreeView)
                If ($iTemp <> $iSelected) and ($iTemp <> 0) Then
                    $sText = _GUICtrlTreeView_GetText($idFileTreeView, $iTemp)
                    If Not ($sText = "") Then
                        $iSelected = $iTemp
                        $sTreePath = _GUICtrlTreeView_GetTree($idFileTreeView, _GUICtrlTreeView_GetParentHandle($idFileTreeView, $iSelected))
                        Debug("Selected: " & $sText)
                        Debug("Path: " & $sTreePath)
                    EndIf
                EndIf
        EndSwitch
    WEnd

EndFunc

Func LoadTreeView()

    If Not _GUICtrlTreeView_BeginUpdate($idFileTreeView) Then ErrMsg("_GUICtrlTreeView_BeginUpdate", -1)

    If Not _GUICtrlTreeView_DeleteAll($idFileTreeView) Then ErrMsg("_GUICtrlTreeView_DeleteAll", -1)

    Local $iParent
    For $i=0 To 20
        $iParent = _GUICtrlTreeView_Add($idFileTreeView, 0, $i)
        For $iSub=0 To 20
            _GUICtrlTreeView_AddChild($idFileTreeView, $iParent, "Sub " & $iSub)
        Next
    Next

    If Not _GUICtrlTreeView_EndUpdate($idFileTreeView) Then ErrMsg("_GUICtrlTreeView_EndUpdate", -1)

EndFunc

Func ErrMsg($sMsg, $iError = @error)
    If $iError Then Debug($sMsg & " Error: " & $iError, "!")
    Return SetError($iError)
EndFunc

Func Debug($sMsg, $sPrefix = Default)
    If $sPrefix = Default Then $sPrefix = "+"
    ConsoleWrite($sPrefix & " " & $sMsg & @CRLF)
EndFunc

I'm not sure if this is just a problem with my system or if I'm doing something wrong when reading/editing the tree view.

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

Link to comment
Share on other sites

Okay, so it always works when I use _GUICtrlTreeView_Create instead. However, now the Tree View doesn't resize

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

Link to comment
Share on other sites

Thanks Zedna! That's perfect, took me a minute to understand all of that, but it's working great now :) 

It won't resize, however... I've tried using Opt("GUIResizeMode") and getting the ControlID to use with GUICtrlSetResizing, but neither work. (The button is able to be resized)

#include <GUIConstants.au3>
#include <GuiTreeView.au3>

Global $hFileTreeView

Main()

Func Main()

    Local $hGUI = GUICreate("Function Docs", 300, 375, -1, -1, BitOR($WS_SIZEBOX, $WS_SYSMENU))

    GUICtrlCreateButton("", 0, 0, 10, 10)
        If Not GUICtrlSetResizing(-1, $GUI_DOCKRIGHT) Then Exit Debug("Failed to make button resizeable")
    Opt("GUIResizeMode", $GUI_DOCKRIGHT + $GUI_DOCKTOP + $GUI_DOCKBOTTOM)
    $hFileTreeView = _GUICtrlTreeView_Create($hGUI, 10, 10, 250, 350)
        ; Get the ControlID from the control's handle
        Local $idFileTreeView = _WinAPI_GetDlgCtrlID($hFileTreeView)
        If $idFileTreeView = 0 Then Exit Debug(_WinAPI_GetLastError() & ": " & _WinAPI_GetLastErrorMessage())
        Debug("TreeView ID: " & $idFileTreeView) ; <-- Prints 10000 and matches Au3Info
        If Not GUICtrlSetResizing($idFileTreeView, $GUI_DOCKRIGHT + $GUI_DOCKTOP + $GUI_DOCKBOTTOM) Then Debug("Failed to make TreeView resizeable")
    Opt("GUIResizeMode", 0)

;~  ; Register Tree View item changes -- Woot!
;~  GUIRegisterMsg($WM_NOTIFY, 'OnNotify')

    GUISetState()

;~  LoadTreeView()

    While True
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
        EndSwitch
    WEnd

EndFunc

; N.B. The old variable was $TVN_SELCHANGED, a W/A was added to use Unicode/Ansi
Func OnNotify($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hWnd, $iMsg, $iwParam, $ilParam

    Local $tNMTV = DllStructCreate($tagNMTREEVIEW, $ilParam)
    Local $hwndFrom = DllStructGetData($tNMTV, 'hWndFrom')
    Local $iCode = DllStructGetData($tNMTV, 'Code')
    Local $hSelected, $sText, $sTreePath, $hParent
    If $hwndFrom = $hFileTreeView And $iCode = $TVN_SELCHANGEDW Then
        $hSelected = DllStructGetData($tNMTV, 'NewhItem')
        $sText = _GUICtrlTreeView_GetText($hFileTreeView, $hSelected)
        
        $hParent = _GUICtrlTreeView_GetParentHandle($hFileTreeView, $hSelected)
        if $hParent = 0 Then Return ; No parent
        
        $sTreePath = _GUICtrlTreeView_GetTree($hFileTreeView, $hParent)
        
        Debug("Text: " & $sText)
        Debug("Path: " & $sTreePath)
    EndIf
EndFunc

Func Debug($sMsg, $sPrefix = Default)
    If $sPrefix = Default Then $sPrefix = "+"
    ConsoleWrite($sPrefix & " " & $sMsg & @CRLF)
EndFunc
Edited by seadoggie01
Added OnNotify code for future reference

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

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...