Rifa Posted July 20, 2011 Share Posted July 20, 2011 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. Link to comment Share on other sites More sharing options...
monoscout999 Posted July 20, 2011 Share Posted July 20, 2011 (edited) 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 July 20, 2011 by monoscout999 Link to comment Share on other sites More sharing options...
Rifa Posted July 20, 2011 Author Share Posted July 20, 2011 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.. Link to comment Share on other sites More sharing options...
monoscout999 Posted July 20, 2011 Share Posted July 20, 2011 (edited) 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 July 20, 2011 by monoscout999 Link to comment Share on other sites More sharing options...
Rifa Posted July 20, 2011 Author Share Posted July 20, 2011 the treeview items is fetched from the database, so.. i use a loop to create the treeview itemsfor 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.expandcollapse popup#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 EndFuncNote that i want to add event to the Treeview item When i Click an item and i didn't give them variable. Link to comment Share on other sites More sharing options...
monoscout999 Posted July 20, 2011 Share Posted July 20, 2011 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.expandcollapse popup#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 EndFuncI 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. Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted July 20, 2011 Moderators Share Posted July 20, 2011 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 selectionAbsolutely 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 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Rifa Posted July 26, 2011 Author Share Posted July 26, 2011 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. expandcollapse popup#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 Link to comment Share on other sites More sharing options...
somedude12 Posted December 27, 2014 Share Posted December 27, 2014 (edited) 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 December 27, 2014 by somedude12 Link to comment Share on other sites More sharing options...
Phillipe Posted June 30, 2015 Share Posted June 30, 2015 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 Jewtus 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now