Jump to content
Renderer

AutoIT TreeView get Current and Old Selection

Recommended Posts

Hello Guys! I have a question regarding TreeView selections. How can I get the OLD and CURRENT selected Items in a TreeView? The script bellow does only get the current TreeView selected Item. 

global $hWin = GUICreate("TreeView --> ListView", 650,550)
global $TreeView = GUICtrlCreateTreeView(0,0,250,550)

;TreeView Items

global $tv_item_1 = GUICtrlCreateTreeViewItem("Item 1", $TreeView)
global $tv_item_2 = GUICtrlCreateTreeViewItem("Item 2", $TreeView)
global $tv_item_3 = GUICtrlCreateTreeViewItem("Item 3", $TreeView)

GUISetState()

global $current_selection
global $old_selection

While 1
   Switch GUIGetMsg()
   case -3

      ExitLoop

   EndSwitch

   local $tv_selection = _GUICtrlTreeView_GetSelection($TreeView); Gets the current TreeView Selection
   Consolewrite("Current Selection: " & $tv_selection & @crlf)
  

WEnd

Thanks in advance

Share this post


Link to post
Share on other sites

Hello, 

I already shared multiple time this register msg. 

I think it can fit to your needs until you understand the GUIRegisterMsg()

Of course, you have to rework on variables. 

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

Global $WMNotifyTest = 1

Func WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)
         If $WMNotifyTest = 1 Then
         ConsoleWrite ("; WMNotify () Is Working!!"&@CRLF)
         $WMNotifyTest += 1
         EndIf
    #forceref $hWnd, $iMsg, $wParam
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndListView, $tInfo
    ; Local $tBuffer
    $hWndListView = $Console
    If Not IsHWnd($Console) Then $hWndListView = GUICtrlGetHandle($Console)

    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")
    Switch $hWndFrom
        Case $hWndListView
            Switch $iCode
                Case $NM_CLICK ; Sent by a list-view control when the user clicks an item with the left mouse button
                    $tInfo = DllStructCreate($tagNMITEMACTIVATE, $lParam)
                    _DebugPrint("$NM_CLICK" & @CRLF & _
                            $TextFav = _GUICtrlListView_GetItemText($Console, DllStructGetData($tInfo, "Index")) & _
                            "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
                            "-->Code:" & @TAB & $iCode & @CRLF & _
                            "-->Index:" & @TAB & DllStructGetData($tInfo, "Index") & @CRLF & _
                            "-->SubItem:" & @TAB & DllStructGetData($tInfo, "SubItem") & @CRLF & _
                            "-->NewState:" & @TAB & DllStructGetData($tInfo, "NewState") & @CRLF & _
                            "-->OldState:" & @TAB & DllStructGetData($tInfo, "OldState") & @CRLF & _
                            "-->Changed:" & @TAB & DllStructGetData($tInfo, "Changed") & @CRLF & _
                            "-->ActionX:" & @TAB & DllStructGetData($tInfo, "ActionX") & @CRLF & _
                            "-->ActionY:" & @TAB & DllStructGetData($tInfo, "ActionY") & @CRLF & _
                            "-->lParam:" & @TAB & DllStructGetData($tInfo, "lParam") & @CRLF & _
                            "-->KeyFlags:" & @TAB & DllStructGetData($tInfo, "KeyFlags"))
                    ; No return value
                Case $NM_DBLCLK ; Sent by a list-view control when the user double-clicks an item with the left mouse button
                    $tInfo = DllStructCreate($tagNMITEMACTIVATE, $lParam)
                    ClipPut(_GUICtrlListView_GetItemText($Console, DllStructGetData($tInfo, "Index")))
                    _DebugPrint("$NM_DBLCLK" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
                            "-->Code:" & @TAB & $iCode & @CRLF & _
                            "-->Index:" & @TAB & DllStructGetData($tInfo, "Index") & @CRLF & _
                            "-->SubItem:" & @TAB & DllStructGetData($tInfo, "SubItem") & @CRLF & _
                            "-->NewState:" & @TAB & DllStructGetData($tInfo, "NewState") & @CRLF & _
                            "-->OldState:" & @TAB & DllStructGetData($tInfo, "OldState") & @CRLF & _
                            "-->Changed:" & @TAB & DllStructGetData($tInfo, "Changed") & @CRLF & _
                            "-->ActionX:" & @TAB & DllStructGetData($tInfo, "ActionX") & @CRLF & _
                            "-->ActionY:" & @TAB & DllStructGetData($tInfo, "ActionY") & @CRLF & _
                            "-->lParam:" & @TAB & DllStructGetData($tInfo, "lParam") & @CRLF & _
                            "-->KeyFlags:" & @TAB & DllStructGetData($tInfo, "KeyFlags"))
                    ; No return value
                Case $NM_RDBLCLK ; Sent by a list-view control when the user double-clicks an item with the right mouse button
                    $tInfo = DllStructCreate($tagNMITEMACTIVATE, $lParam)
                    _GUICtrlListView_DeleteItem($Console, DllStructGetData($tInfo, "Index"))

                    _FileWriteToLine ( @ScriptDir&"\Log.txt" , DllStructGetData($tInfo, "Index") , "" , True , True )
                    _FileWriteToLine ( @ScriptDir&"\Data\Log02.txt" , DllStructGetData($tInfo, "Index") , "" , True , True )
                    _FileWriteToLine ( @ScriptDir&"\Data\Log002.txt" , DllStructGetData($tInfo, "Index") , "" , True , True )
                    _FileWriteToLine ( @ScriptDir&"\Log.txt" , DllStructGetData($tInfo, "Index") , "" , True , True )
                    _FileWriteToLine ( @ScriptDir&"\Data\Log02.txt" , DllStructGetData($tInfo, "Index") , "" , True , True )
                    _FileWriteToLine ( @ScriptDir&"\Data\Log002.txt" , DllStructGetData($tInfo, "Index") , "" , True , True )
                    _FileWriteToLine ( @ScriptDir&"\Log.txt" , DllStructGetData($tInfo, "Index") , "" , True , True )
                    _FileWriteToLine ( @ScriptDir&"\Data\Log02.txt" , DllStructGetData($tInfo, "Index") , "" , True , True )
                    _FileWriteToLine ( @ScriptDir&"\Data\Log002.txt" , DllStructGetData($tInfo, "Index") , "" , True , True )

                    _DebugPrint("$NM_RDBLCLK" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
                            "-->Code:" & @TAB & $iCode & @CRLF & _
                            "-->Index:" & @TAB & DllStructGetData($tInfo, "Index") & @CRLF & _
                            "-->SubItem:" & @TAB & DllStructGetData($tInfo, "SubItem") & @CRLF & _
                            "-->NewState:" & @TAB & DllStructGetData($tInfo, "NewState") & @CRLF & _
                            "-->OldState:" & @TAB & DllStructGetData($tInfo, "OldState") & @CRLF & _
                            "-->Changed:" & @TAB & DllStructGetData($tInfo, "Changed") & @CRLF & _
                            "-->ActionX:" & @TAB & DllStructGetData($tInfo, "ActionX") & @CRLF & _
                            "-->ActionY:" & @TAB & DllStructGetData($tInfo, "ActionY") & @CRLF & _
                            "-->lParam:" & @TAB & DllStructGetData($tInfo, "lParam") & @CRLF & _
                            "-->KeyFlags:" & @TAB & DllStructGetData($tInfo, "KeyFlags"))
;~                          FileClose ($File2)
;~                          FileClose ($File02)
;~                          FileClose ($File002)
                    ; No return value
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

 


My video tutorials : ( In construction )  || My Discord : https://discord.gg/S9AnwHw

How to Ask Help ||  UIAutomation From Junkew || WebDriver From Danp2 || And Water's UDFs in the Quote

Spoiler

 Water's UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites

Or just modifying your code a bit :

#include <GuiTreeView.au3>

Global $hWin = GUICreate("TreeView --> ListView", 650, 550)
Global $TreeView = GUICtrlCreateTreeView(0, 0, 250, 550)

;TreeView Items

Global $tv_item_1 = GUICtrlCreateTreeViewItem("Item 1", $TreeView)
Global $tv_item_2 = GUICtrlCreateTreeViewItem("Item 2", $TreeView)
Global $tv_item_3 = GUICtrlCreateTreeViewItem("Item 3", $TreeView)

GUISetState()

Global $current_selection = 0, $tv_selection, $old_selection

SetSelection ()

While 1
  Switch GUIGetMsg()
    Case -3
      ExitLoop
    Case $tv_item_1 To $tv_item_3
      SetSelection()
  EndSwitch
WEnd

Func SetSelection ()
  $tv_selection = _GUICtrlTreeView_GetSelection($TreeView)
  If $current_selection <> $tv_selection Then
    $old_selection = $current_selection
    $current_selection = $tv_selection
    ConsoleWrite("Current Selection: " & $tv_selection & "  Old Selection = " & $old_selection & @CRLF)
  EndIf
EndFunc

This way you won't overload the CPU by always searching for the new selection...

 

Edited by Nine

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

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...