Jump to content

TreeView (UDF) problem (very strange unconsistent behaviour)

Recommended Posts

Hi guys!

Backgroud: I have a TreeView that contains Dates as parent items, and in each day, events (child items). I'm trying to create a function that will either add an event (and his date parent), or update an event's sub-item if it already exists (searched by lParam).

But I have a strage behaviour when searching the treeview items (_GetFirstChild, _GetNextChild). See the code :

#include <GuiTreeView.au3>

$hGUI = GUICreate("Test", 400, 300)
$hTV = GUICtrlCreateTreeView(8, 8, 384, 284)

__guiTv_addOrUpdateItem($hTV, "Date 1", "Item 1", 10)
__guiTv_addOrUpdateItem($hTV, "Date 1", "Item 2", 20)
__guiTv_addOrUpdateItem($hTV, "Date 1", "Item 3", 30)

; normaly, this should change sub-item 2 text
__guiTv_addOrUpdateItem($hTV, "Date 1", "Changed!", 20)

While 1
    $iMsg = GUIGetMsg()
    Switch $iMsg
        Case -3

Func __guiTv_addOrUpdateItem($hTV, $sMainText, $sSubText, $iParam, $hParentItem = Default)
    ; find or create parent item
    If $hParentItem == Default Then
        $hParentItem = _GUICtrlTreeView_FindItem($hTV, $sMainText)
        If $hParentItem == 0 Then $hParentItem = _GUICtrlTreeView_InsertItem($hTV, $sMainText, 0, $TVI_SORT)
    ConsoleWrite("ParentItem: " & $hParentItem & " (" & _GUICtrlTreeView_GetText($hTV, $hParentItem) & ")" & @CRLF)

    ; see if there's already a sub-item with $iParam
    Local $hChild = _GUICtrlTreeView_GetFirstChild($hTV, $hParentItem)
    While $hChild
        ConsoleWrite("Check child: " & $hChild & " (" & _GUICtrlTreeView_GetText($hTV, $hChild) & ")" & @CRLF)
        If _GUICtrlTreeView_GetItemParam($hTV, $hChild) == $iParam Then
            ; if found: change it's text and return
            _GUICtrlTreeView_SetText($hTV, $hChild, $sSubText)
            Return $hChild
        $hChild = _GUICtrlTreeView_GetNextChild($hTV, $hChild)

    ; create new sub-item
    $hChild = _GUICtrlTreeView_AddChild($hTV, $hParentItem, $sSubText)
    _GUICtrlTreeView_SetItemParam($hTV, $hChild, $iParam)
    Return $hChild

2 problems :

- First: there is a totally inconsistent behaviour when calling the _GetFirstChild function: sometimes it returns good, sometimes not! (run the script many times and see the console output)

- Second: the _GetItemParam function doesn't work! Always returning 0

I'll be very thankfull for any help :)


Edit: Problem solved (I think): _GuiCtrlTreeView_ functions doesn't work very well with control IDs. I did this and it worked

$hTV = GUICtrlGetHandle(GUICtrlCreateTreeView(8, 8, 384, 284))

$hTV = GUICtrlGetHandle(GUICtrlCreateTreeView(8, 8, 384, 284))

But, why!?

Edited by matwachich
Link to post
Share on other sites

If you look at the remark of _GUICtrlTreeView_SetItemParam, it says :

Warning: Do not use this function on items created with GUICtrlCreateTreeViewItem().

But it should also says that it should not be used with tree view created with GUICtrlCreateTreeView because it returns ID instead of handle.

The way you solved your issue with GUICtrlGetHandle, it translates ID into handle, so this works fine.  Or you could simply use _GUICtrlTreeView_Create that is returning a handle.

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
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...