Jump to content
Sign in to follow this  
Rifa

How to get text from a treeview item when its clicked

Recommended Posts

Rifa

I have a problem with treeview items, how to add click event to the items when i created treeview items dynamically inside a loop.

I've tried with WM_NOTIFY.. but when i click the items it returned wrong value.. It return the text before the selected item.

Share this post


Link to post
Share on other sites
monoscout999

post your tests.

EDITED:

$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
                    $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", "Current item selected is: " & $text)  ; $advmsg[0] contains the text and $advmsg[1] the state value of the treeview item
                    EndIf
                EndIf

This was taken from the GuiCtrlCreateTreeView example from the help.

Edited by monoscout999

Share this post


Link to post
Share on other sites
Rifa

post your tests.

EDITED:

$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
                    $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", "Current item selected is: " & $text)  ; $advmsg[0] contains the text and $advmsg[1] the state value of the treeview item
                    EndIf
                EndIf

This was taken from the GuiCtrlCreateTreeView example from the help.

i am sorry, i can't post the code from my mobile phone.. I can't log in since my ip get blocked (maybe because i use dialup modem).

But soon i'll post my code..

Share this post


Link to post
Share on other sites
monoscout999

Look in the help file for GuiCtrlCreateTreeView and run the example it takes the text from the selected item when you click the "info" button. maybe is that what you are looking for.

It only uses GuiCtrlRead()

Edited by monoscout999

Share this post


Link to post
Share on other sites
Rifa

the treeview items is fetched from the database, so.. i use a loop to create the treeview items

for example, below is an example GUI with a treeview and listview. the WM_NOTIFY method was working for the listview items, but when i applied to the treeview it also worked but return the wrong value.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <StructureConstants.au3>
#include <listviewconstants.au3>

#region - GUI Create
GUICreate('treeview',550,400)
$TreeView1=GUICtrlCreateTreeView(10,10,200,380)
$item1=GUICtrlCreateTreeViewItem("Root Item 1",$TreeView1)
$item2=GUICtrlCreateTreeViewItem("Root Item 2",$TreeView1)
for $i=1 to 10
    GUICtrlCreateTreeViewItem("Child Item "&$i,$item1 )
    GUICtrlCreateTreeViewItem("Child Item "&$i,$item2 )
Next
$ListView1=GUICtrlCreateListView("test|test",220,10,320,380)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 150)
for $i=1 to 50
    GUICtrlCreateListViewItem("List Item"&$i&"|List Item "&$i&" Column 2",$ListView1)
Next

GUISetState()
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
#endregion

#region - GUI SelectLoop
While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case  $GUI_EVENT_CLOSE
            Exit
        Case $TreeView1
                ;MsgBox(64,"",GUICtrlRead($TreeView1,1))   ; This way isn't work.
                $data=GUICtrlRead($TreeView1)
                ConsoleWrite($data)
                if $data=0 Then
                    ConsoleWrite("No Item Selected")
                Else
                    ConsoleWrite("Item Selected")
                EndIf
        Case $ListView1
    EndSwitch
WEnd


;Second Try, it's work for the list view item, and it work for the treeview items too but it returned wrong value :(.
;the value returned is the selected item before new item selected/clicked
;i've checked the $tagNMTREEVIEW, but there isn't same with the $tagNMLISTVIEW, there is no item param.

Func WM_NOTIFY($hWnd, $Msg, $wParam, $lParam)
    Local $tNMHDR, $IdFrom, $iCode

    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $IdFrom = DllStructGetData($tNMHDR, "IdFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")

    Switch $IdFrom
        Case $ListView1
            Switch $iCode
                Case $NM_CLICK
                    Local $tInfo = DllStructCreate($tagNMLISTVIEW, $lParam)
                    Local $iItem = DllStructGetData($tInfo, "Item")

                    If $iItem <> -1 Then
                        $data=GUICtrlRead(GUICtrlRead($ListView1))
                        ConsoleWrite($data&@CR)
                    EndIf

            EndSwitch
        Case $TreeView1
            Switch $iCode
                Case $NM_CLICK
                    Local $tInfo = DllStructCreate($tagTVITEMEX , $lParam)
                    Local $iState = DllStructGetData($tInfo, "State")

                            $data=GUICtrlRead($TreeView1,1)
                            ConsoleWrite("!"&$data&@CR)    ;Write treeview item text/caption to the console, try to view the result into the message box below.
                            ;MsgBox(GUICtrlRead($TreeView1,1))

                    Return 0
        EndSwitch
    EndSwitch

    Return $GUI_RUNDEFMSG
EndFunc

Note that i want to add event to the Treeview item When i Click an item and i didn't give them variable.

Share this post


Link to post
Share on other sites
monoscout999

The credits go for SkellySoul i already sees what you want to do from one of his posts

I post you here a Short version.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <StructureConstants.au3>
#include <listviewconstants.au3>
#include <GUITreeView.au3>
global $iTreeView_Flag = false
#region - GUI Create
GUICreate('treeview',550,400)
$TreeView1=GUICtrlCreateTreeView(10,10,200,380)
$hTreeView = GUICtrlGetHandle($TreeView1)
$item1=GUICtrlCreateTreeViewItem("Root Item 1",$TreeView1)
$item2=GUICtrlCreateTreeViewItem("Root Item 2",$TreeView1)
for $i=1 to 10
    GUICtrlCreateTreeViewItem("Child Item "&$i,$item1 )
    GUICtrlCreateTreeViewItem("Child Item "&$i,$item2 )
Next
$ListView1=GUICtrlCreateListView("test|test",220,10,320,380)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 150)
for $i=1 to 50
    GUICtrlCreateListViewItem("List Item"&$i&"|List Item "&$i&" Column 2",$ListView1)
Next

GUISetState()
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
#endregion

#region - GUI SelectLoop
While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case  $GUI_EVENT_CLOSE
            Exit
;~         Case $TreeView1
;~                 ;MsgBox(64,"",GUICtrlRead($TreeView1,1))   ; This way isn't work.
;~                 $data=GUICtrlRead($TreeView1)
;~                 ConsoleWrite($data)
;~                 if $data=0 Then
;~                     ConsoleWrite("No Item Selected")
;~                 Else
;~                     ConsoleWrite("Item Selected")
;~                 EndIf
;~         Case $ListView1
    EndSwitch
    If $iTreeView_Flag Then
        $iTreeView_Flag = False
        ConsoleWrite(_GUICtrlTreeView_GetText($hTreeView, _GUICtrlTreeView_GetSelection($hTreeView)) & @CRLF)
    EndIf
WEnd


;Second Try, it's work for the list view item, and it work for the treeview items too but it returned wrong value :(.
;the value returned is the selected item before new item selected/clicked
;i've checked the $tagNMTREEVIEW, but there isn't same with the $tagNMLISTVIEW, there is no item param.

Func WM_NOTIFY($hWnd, $Msg, $wParam, $lParam)
    Local $tNMHDR, $IdFrom, $iCode

    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $IdFrom = DllStructGetData($tNMHDR, "IdFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")
        Switch $hWndFrom
        Case $hTreeView
            Switch $iCode
                Case $NM_CLICK
                     $iTreeView_Flag = True
            EndSwitch
    EndSwitch
;~     Switch $IdFrom
;~         Case $ListView1
;~             Switch $iCode
;~                 Case $NM_CLICK
;~                     Local $tInfo = DllStructCreate($tagNMLISTVIEW, $lParam)
;~                     Local $iItem = DllStructGetData($tInfo, "Item")

;~                     If $iItem <> -1 Then
;~                         $data=GUICtrlRead(GUICtrlRead($ListView1))
;~                         ConsoleWrite($data&@CR)
;~                     EndIf

;~             EndSwitch
;~         Case $TreeView1
;~             Switch $iCode
;~              Case $NM_CLICK
;~                     Local $tInfo = DllStructCreate($tagTVITEMEX , $lParam)
;~                     Local $iState = DllStructGetData($tInfo, "State")

;~                             $data=GUICtrlRead($TreeView1,1)
;~                             ConsoleWrite("!"&$data&@CR)    ;Write treeview item text/caption to the console, try to view the result into the message box below.
                            ;MsgBox(GUICtrlRead($TreeView1,1))

;~                     Return 0
;~         EndSwitch
;~     EndSwitch

    Return $GUI_RUNDEFMSG
EndFunc

I guess that the problem before is that NOTIFY is getting the selected item from the treeview but the selected item before the change of selection. It requieres a little of time that change, it`s something odd, if you do the consolewrite instead the $iTreeView_Flag flag method you will see that behavior.

Sorry my bad english i hope that this is what you want.

Share this post


Link to post
Share on other sites
Melba23

monoscout999,

I guess that the problem before is that NOTIFY is getting the selected item from the treeview but the selected item before the change of selection

Absolutely correct! :)

That is why you need to use the flag in this case to allow the new selection to happen before trying to read the TreeView. On a separate note, remember that if the code in a message handler is anything other than minimal you should always use a flag to run it because (as the Help file clearly states):

"the return to the system should be as fast as possible !!!" ;)

M23


Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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
Rifa

The credits go for SkellySoul i already sees what you want to do from one of his posts

I post you here a Short version.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <StructureConstants.au3>
#include <listviewconstants.au3>
#include <GUITreeView.au3>
global $iTreeView_Flag = false
#region - GUI Create
GUICreate('treeview',550,400)
$TreeView1=GUICtrlCreateTreeView(10,10,200,380)
$hTreeView = GUICtrlGetHandle($TreeView1)
$item1=GUICtrlCreateTreeViewItem("Root Item 1",$TreeView1)
$item2=GUICtrlCreateTreeViewItem("Root Item 2",$TreeView1)
for $i=1 to 10
    GUICtrlCreateTreeViewItem("Child Item "&$i,$item1 )
    GUICtrlCreateTreeViewItem("Child Item "&$i,$item2 )
Next
$ListView1=GUICtrlCreateListView("test|test",220,10,320,380)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 150)
for $i=1 to 50
    GUICtrlCreateListViewItem("List Item"&$i&"|List Item "&$i&" Column 2",$ListView1)
Next

GUISetState()
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
#endregion

#region - GUI SelectLoop
While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case  $GUI_EVENT_CLOSE
            Exit
;~         Case $TreeView1
;~                 ;MsgBox(64,"",GUICtrlRead($TreeView1,1))   ; This way isn't work.
;~                 $data=GUICtrlRead($TreeView1)
;~                 ConsoleWrite($data)
;~                 if $data=0 Then
;~                     ConsoleWrite("No Item Selected")
;~                 Else
;~                     ConsoleWrite("Item Selected")
;~                 EndIf
;~         Case $ListView1
    EndSwitch
    If $iTreeView_Flag Then
        $iTreeView_Flag = False
        ConsoleWrite(_GUICtrlTreeView_GetText($hTreeView, _GUICtrlTreeView_GetSelection($hTreeView)) & @CRLF)
    EndIf
WEnd


;Second Try, it's work for the list view item, and it work for the treeview items too but it returned wrong value :(.
;the value returned is the selected item before new item selected/clicked
;i've checked the $tagNMTREEVIEW, but there isn't same with the $tagNMLISTVIEW, there is no item param.

Func WM_NOTIFY($hWnd, $Msg, $wParam, $lParam)
    Local $tNMHDR, $IdFrom, $iCode

    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $IdFrom = DllStructGetData($tNMHDR, "IdFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")
        Switch $hWndFrom
        Case $hTreeView
            Switch $iCode
                Case $NM_CLICK
                     $iTreeView_Flag = True
            EndSwitch
    EndSwitch
;~     Switch $IdFrom
;~         Case $ListView1
;~             Switch $iCode
;~                 Case $NM_CLICK
;~                     Local $tInfo = DllStructCreate($tagNMLISTVIEW, $lParam)
;~                     Local $iItem = DllStructGetData($tInfo, "Item")

;~                     If $iItem <> -1 Then
;~                         $data=GUICtrlRead(GUICtrlRead($ListView1))
;~                         ConsoleWrite($data&@CR)
;~                     EndIf

;~             EndSwitch
;~         Case $TreeView1
;~             Switch $iCode
;~              Case $NM_CLICK
;~                     Local $tInfo = DllStructCreate($tagTVITEMEX , $lParam)
;~                     Local $iState = DllStructGetData($tInfo, "State")

;~                             $data=GUICtrlRead($TreeView1,1)
;~                             ConsoleWrite("!"&$data&@CR)    ;Write treeview item text/caption to the console, try to view the result into the message box below.
                            ;MsgBox(GUICtrlRead($TreeView1,1))

;~                     Return 0
;~         EndSwitch
;~     EndSwitch

    Return $GUI_RUNDEFMSG
EndFunc

it's work, thanks mono, so we need to get the next item index to get the tree item text :)

Share this post


Link to post
Share on other sites
somedude12

you probably need to use $TVN_SELCHANGEDW, so GUICtrlRead($wParam) will return your selected control id.

It's an old post but I thought maybe someone can use the info.

Edited by somedude12

Share this post


Link to post
Share on other sites
Phillipe

Here is a even short way:

#include <GUITreeView.au3>
#include <TreeViewConstants.au3>
#include <WindowsConstants.au3>

Opt("GUIOnEventMode", 1)

$hGUI = GUICreate("Tree", 300, 300, 300, 300)

Local $idTreeview = GUICtrlCreateTreeView(3, 3, 300, 300, BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS), $WS_EX_CLIENTEDGE)
$hTreeView = GUICtrlGetHandle($idTreeview)

Local $idAppItem = GUICtrlCreateTreeViewItem('button', $idTreeview)
GUICtrlSetOnEvent($idAppItem,"treeViewPress")

GUISetState(@SW_SHOW, $hGUI)

Func manager()
   While 1

    WEnd
EndFunc

manager()

Func treeViewPress()
    ConsoleWrite(_GUICtrlTreeView_GetText($hTreeView, _GUICtrlTreeView_GetSelection($hTreeView)) & @CRLF)
EndFunc

:D

  • Like 1

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  

×