Jump to content
langthang084

Cant read Treeview Item?

Recommended Posts

langthang084
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <TreeViewConstants.au3>
#include <WindowsConstants.au3>
#include <GuiTreeView.au3>
#include <Array.au3>
$Child = 9999

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 368, 438, 192, 124)
$TreeView1 = GUICtrlCreateTreeView(45, 36, 199, 322)
$Button1 = GUICtrlCreateButton("CREATE", 255, 39, 100, 43)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

Local $ArrayTreeView[][] = [['', 'Item 1', ''], _
                           ['', '', 'Item 1.1'], _
                           ['', '', 'Item 1.2'], _
                           ['', 'Item 2', ''], _
                           ['', 'Item 3', ''], _
                           ['', '', 'Item 3.1']]

_GUICtrlTreeView_BeginUpdate($TreeView1)
$iParam = 1
$CurrentItem = ''
for $a = 0 to UBound($ArrayTreeView) - 1
   if $ArrayTreeView[$a][1] <> '' then
      $ArrayTreeView[$a][0] = _GUICtrlTreeView_Add($TreeView1, 0, $ArrayTreeView[$a][1] )
      _GUICtrlTreeView_SetItemParam($TreeView1, $ArrayTreeView[$a][0], $iParam)
      $CurrentItem = $ArrayTreeView[$a][0]
      $iParam += 1
   Else
      if $ArrayTreeView[$a][2] <> '' then
         $ArrayTreeView[$a][0] = _GUICtrlTreeView_AddChild($TreeView1, $CurrentItem, $ArrayTreeView[$a][2] )
         _GUICtrlTreeView_SetItemParam($TreeView1, $ArrayTreeView[$a][0], $iParam)
         $iParam += 1
      Else
         $CurrentItem = ''
      EndIf
   EndIf


Next
_GUICtrlTreeView_EndUpdate($TreeView1)
_ArrayDisplay($ArrayTreeView)
While 1
    $nMsg = GUIGetMsg(1)
    Switch $nMsg[1]
    case $Form1
       Switch $nMsg[0]
        Case $GUI_EVENT_CLOSE
            Exit
         case GUICtrlRead($TreeView1)
            $ID = GUICtrlRead($TreeView1)
            MsgBox(0, '', $ID)

         case $Button1
            _CreateChildGui()
         EndSwitch

      case $Child
         Switch $nMsg[0]
         Case $GUI_EVENT_CLOSE
            GUIDelete($Child)

         EndSwitch


    EndSwitch
WEnd


Func _CreateChildGui()
   $Child = GUICreate("Child", 368, 438, 100, 124)
   GUISetState(@SW_SHOW)
EndFunc

Why I cant read treeview Items?????

Share this post


Link to post
Share on other sites
Melba23

langthang084,

 Firstly, GUICtrlRead is not an event trapped by GUIGetMsg - you need to use a Windows handler to detect the click. Secondly you need to use the "advanced" mode of GUICtrlRead to get the text of a TreeView item, as explained in the Help file. See this amended script:

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <TreeViewConstants.au3>
#include <WindowsConstants.au3>
#include <GuiTreeView.au3>
#include <Array.au3>
$Child = 9999

#Region ### START Koda GUI section ### Form=
    $Form1 = GUICreate("Form1", 368, 438, 192, 124)
    $TreeView1 = GUICtrlCreateTreeView(45, 36, 199, 322)
    $Button1 = GUICtrlCreateButton("CREATE", 255, 39, 100, 43)
    GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

Local $ArrayTreeView[][] = [['', 'Item 1', ''], _
        ['', '', 'Item 1.1'], _
        ['', '', 'Item 1.2'], _
        ['', 'Item 2', ''], _
        ['', 'Item 3', ''], _
        ['', '', 'Item 3.1']]

_GUICtrlTreeView_BeginUpdate($TreeView1)
$iParam = 1
$CurrentItem = ''
For $a = 0 To UBound($ArrayTreeView) - 1
    If $ArrayTreeView[$a][1] <> '' Then
        $ArrayTreeView[$a][0] = _GUICtrlTreeView_Add($TreeView1, 0, $ArrayTreeView[$a][1])
        _GUICtrlTreeView_SetItemParam($TreeView1, $ArrayTreeView[$a][0], $iParam)
        $CurrentItem = $ArrayTreeView[$a][0]
        $iParam += 1
    Else
        If $ArrayTreeView[$a][2] <> '' Then
            $ArrayTreeView[$a][0] = _GUICtrlTreeView_AddChild($TreeView1, $CurrentItem, $ArrayTreeView[$a][2])
            _GUICtrlTreeView_SetItemParam($TreeView1, $ArrayTreeView[$a][0], $iParam)
            $iParam += 1
        Else
            $CurrentItem = ''
        EndIf
    EndIf


Next
_GUICtrlTreeView_EndUpdate($TreeView1)

; Create a dummy to react to the click
$cClick_Dummy = GUICtrlCreateDummy()

_ArrayDisplay($ArrayTreeView)

; Register the Windows message handler
GUIRegisterMsg($WM_NOTIFY, "_WM_NOTIFY")

While 1
    $nMsg = GUIGetMsg(1)
    Switch $nMsg[1]
        Case $Form1
            Switch $nMsg[0]
                Case $GUI_EVENT_CLOSE
                    Exit

                Case $cClick_Dummy ; GUICtrlRead($TreeView1)
                    $ID = GUICtrlRead($TreeView1, 1)
                    MsgBox(0, '', $ID)

                Case $Button1
                    _CreateChildGui()
            EndSwitch

        Case $Child
            Switch $nMsg[0]
                Case $GUI_EVENT_CLOSE
                    GUIDelete($Child)

            EndSwitch


    EndSwitch
WEnd


Func _CreateChildGui()
    $Child = GUICreate("Child", 368, 438, 100, 124)
    GUISetState(@SW_SHOW)
EndFunc   ;==>_CreateChildGui

Func _WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)
    ; Create NMTREEVIEW structure
    Local $tStruct = DllStructCreate("struct;hwnd hWndFrom;uint_ptr IDFrom;INT Code;endstruct;" & _
            "uint Action;struct;uint OldMask;handle OldhItem;uint OldState;uint OldStateMask;" & _
            "ptr OldText;int OldTextMax;int OldImage;int OldSelectedImage;int OldChildren;lparam OldParam;endstruct;" & _
            "struct;uint NewMask;handle NewhItem;uint NewState;uint NewStateMask;" & _
            "ptr NewText;int NewTextMax;int NewImage;int NewSelectedImage;int NewChildren;lparam NewParam;endstruct;" & _
            "struct;long PointX;long PointY;endstruct", $lParam)
    Local $hWndFrom = DllStructGetData($tStruct, "hWndFrom")
    Local $iCode = DllStructGetData($tStruct, "Code")

    ; if it is our TreeView
    If $hWndFrom = GUICtrlGetHandle($TreeView1) Then
        ; Check event
        Switch $iCode
            Case -2 ; $NM_CLICK
                ; Fire the dummy control
                GUICtrlSendToDummy($cClick_Dummy)
        EndSwitch
    EndIf
EndFunc

Please ask if you have any questions.

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
langthang084

Yeah! M23

Thanks U very much!!

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

×