Jump to content
Sign in to follow this  
matrix200

Question regarding treeview control

Recommended Posts

In my script I need to detect when a treeview control has focus (is clicked on) or keyboard is used to traverse the items.

Then I need to detect the path of the selected item and do some operation with it (It is a full ups variable name that I am sending to the UPS for getting its value).

Now the problem is when I use the GuiTreeView , I can't detect the ControlId of it and therefore have no way of knowing that indeed TreeViewItem was clicked.

On the other hand if I use GUICtrlCreateTreeViewItem , I have no way of knowing the ControlId of an item using its path .

I need that for recursively creating the path in tree.

For example say I have the following path "Root|Child1|Child2" that I want to add.

Using recursion I need to first look if such path already exists and have its ControlId and there is no way of doing that.

What are my options?

Sure I can have it resolved using the Adlib and constantly polling for selected items on the tree and doing all the stuff.

I am not liking that since I have to give up another Adlib which is more important (monitoring UPS variables for proper shutdown in case of blackout) and for performance reasons because I need to send lots of chatter on TCP and interpert its results.

It is preferable to perform only if selection changes on the tree.

Share this post


Link to post
Share on other sites

Hi,

....

On the other hand if I use GUICtrlCreateTreeViewItem , I have no way of knowing the ControlId of an item using its path .

I need that for recursively creating the path in tree.

For example say I have the following path "Root|Child1|Child2" that I want to add.

Using recursion I need to first look if such path already exists and have its ControlId and there is no way of doing that.

What are my options?

....

You can get the control id from the path
#include <GUIConstants.au3>
#Include <GuiTreeView.au3>
Opt("GUIOnEventMode", 1)

$Gui = GUICreate("My GUI with treeview", 280, 270)
$GCCTV = GUICtrlCreateTreeView(5, 5, 270, 200)
For $i = 1 To 5
    GUICtrlCreateTreeViewItem("Root " & $i, $GCCTV)
    GUICtrlSetOnEvent(-1, "Event")
    GUICtrlCreateTreeViewItem("Child " & $i & "-1", -1)
    GUICtrlSetOnEvent(-1, "Event")
    GUICtrlCreateTreeViewItem("Child " & $i & "-2", -1)
    GUICtrlSetOnEvent(-1, "Event")
Next
$Input = GUICtrlCreateInput("Root 3|Child 3-1|Child 3-2", 5, 210, 270, 20)
$Button = GUICtrlCreateButton("Find Control ID by Path..", 5, 245, 270, 20)
GUICtrlSetOnEvent(-1, "Event")
GUISetOnEvent($GUI_EVENT_CLOSE, "Close", $Gui)
GUISetState()

While 1
    Sleep(100)
WEnd    

Func Event()
    Switch @GUI_CtrlId
        Case $Button
            $FindEx = _GUICtrlTreeView_FindItemEx($GCCTV, GUICtrlRead($Input))
            If $FindEx Then
                GUICtrlSetOnEvent(_TVGetItemID($GCCTV, $FindEx), "")
                GUICtrlSetState(_TVGetItemID($GCCTV, $FindEx), $GUI_FOCUS)
                GUICtrlSetOnEvent(_TVGetItemID($GCCTV, $FindEx), "Event")
                MsgBox(0, GUICtrlRead($Input), "Control ID from TreePath: " & _TVGetItemID($GCCTV, $FindEx))
            Else
                MsgBox(0, '', "Nothing matches your search")
            EndIf
        Case Else
            MsgBox(0, _GUICtrlTreeView_GetText($GCCTV, @GUI_CtrlId), "Control ID: " & @GUI_CtrlId)
        EndSwitch   
EndFunc

; Get the TVItem Ctrl ID from it's handle 
; $i_treeview = Treview Control ID (Internal TreeView Only!)
; $h_item = Handle to TreeViewItem (Internal TreeView Item Only!)
Func _TVGetItemID($i_treeview, $h_item)
    If $h_item = 0 Then Return 0
    Local $st_TVITEM = DllStructCreate("uint;dword;uint;uint;ptr;int;int;int;int;dword")
    DllStructSetData($st_TVITEM, 1, $TVIF_PARAM)
    DllStructSetData($st_TVITEM, 2, $h_item)
    DllStructSetData($st_TVITEM, 10, 0)
    If Not GUICtrlSendMsg($i_treeview, $TVM_GETITEM, 0, DllStructGetPtr($st_TVITEM)) Then Return 0
    Return DllStructGetData($st_TVITEM, 10)
EndFunc   ;==>_TVGetItemID

Func Close()
    Exit
EndFunc
The _TVGetItemID() function will only work with internally created listview/ items.

Cheers

Share this post


Link to post
Share on other sites

Thanks a lot smashly!

That worked like a charm :)

You just earned yourself a mention in the thanks section of the readme file of the next version ^_^

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  

×
×
  • Create New...