Jump to content

Help in Treeview Checkboxes


Recommended Posts

Hey Frends,

i was able to do it till here, But i am stuck again., can any 1 please Help me out,

As you can see in the below code , i made a gui which is able to show the list of files from Directory using file list to array. and adding checkboxes using treeview style. now i want a little help.,

If i select file/files it should get count of no. of files chosen. and the directory should copied in any empty memory so as to execute those directories path later in script.

Below i am linking a post wrote by rover hu helped me out but in a diffrent way, m so much confused.

as in that link he helped me out for combo Box ctrl pattern which copy the file location selected but, in the below code i want the file path should be copied for Selected Checkboxes. :)

#include <file.au3>
#include <GUIConstantsEx.au3>
#include <TreeViewConstants.au3>
#include <array.au3>
#include <GuiTreeView.au3>
#include <GuiListBox.au3>



Main()


Func Main()

$Main_Window = GUICreate("Checkboxes Select With File List", 413, 298, 302, 218)
$Listview1 = GUICtrlCreateTreeView(16, 16, 377, 265, $TVS_CHECKBOXES)
GUISetState(@SW_SHOW)


dim $file, $dir

Local $aSrcFolder[1] = [0]


$dir = @WindowsDir
$FileList = _FileListToArray($dir,"*.exe",1)
If @error = 1 Or @error = 4 Then Exit MsgBox(0, "", "Folders Not Found or No Files Found.")

For $INDEX = 1 To $FileList[0]
$POINT = StringInStr($FileList[$INDEX],".",0,-1)

If $POINT <> 0 Then

GUICtrlCreateTreeViewItem($FileList[$INDEX], $LISTVIEW1)
;GUICtrlSetData(-1, $FILE[$INDEX])

EndIf
Next



While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Listview1

EndSwitch
WEnd
EndFunc

I know its a bit complicated but its eating me, i have some deadline in office i need to do it.. so i thought to ask your guyz Help.

please its little urgent :)

Link to comment
Share on other sites

Did you mean something so?

#include <file.au3>
#include <GUIConstantsEx.au3>
#include <TreeViewConstants.au3>
#include <array.au3>
#include <GuiTreeView.au3>
#include <GuiListBox.au3>




Main()


Func Main()

$Main_Window = GUICreate("Checkboxes Select With File List", 413, 298, 302, 218)
$Listview1 = GUICtrlCreateTreeView(16, 16, 377, 265, $TVS_CHECKBOXES)
GUISetState(@SW_SHOW)


dim $file, $dir

Local $aSrcFolder[1] = [0]


$dir = @WindowsDir
$FileList = _FileListToArray($dir,"*.exe",1)
If @error = 1 Or @error = 4 Then Exit MsgBox(0, "", "Folders Not Found or No Files Found.")

For $INDEX = 1 To $FileList[0]
$POINT = StringInStr($FileList[$INDEX],".",0,-1)

If $POINT <> 0 Then


GUICtrlCreateTreeViewItem(@WindowsDir & ""&$FileList[$INDEX], $LISTVIEW1)
;GUICtrlSetData(-1, $FILE[$INDEX])

EndIf
Next



While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Listview1

EndSwitch
WEnd
EndFunc
Edited by Danyfirex
Link to comment
Share on other sites

I know its a bit complicated but its eating me, i have some deadline in office i need to do it.. so i thought to ask your guyz Help.

please its little urgent :)

This is not a service, we are all volunteers here.

and don't plead...

How does that old office cubicle sign go again?

'Lack of planning on your part does not constitute a crisis on my part'

I've always liked what Douglas Adams said about deadlines:

'I love deadlines. I like the whooshing sound they make as they fly by'

Douglas Adams - English humorist & science fiction novelist (1952 - 2001)

Spend more time with the help file and forum reading rather than leaning on help from the volunteers.

When you make promises to the office to deliver on something it can come back to bite you on the ass.

Code development takes time and things always go wrong and need time to be debugged, and some things you want to do have a steep learning curve

that you wont find out until you've already promised to deliver.

Above all don't promise things when you are just starting coding...

Don't let your reach exceed your grasp.

That being said, the method I showed you for the combobox can be used for a treeview and a listview

The other way of doing this requires searching through a global 2D array of all files added to the treeview with their folder paths then matching each checked items text to a matching array element

then getting the path from that array elements subitem.

There are examples of the file array on the forum, but not I think of this method

so, I guess its up to me to post another example using the treeview.

This methods global array only has source folder paths in it, and no array searching is required.

Note: Using this method you can't use GUICtrlCreateTreeViewItem() to add items.

AutoIt uses the item param data to store the index pseudo control ID of the item when an item is added with GUICtrlCreateTreeViewItem()

Only use _GUICtrlTreeView_Add***, _GUICtrlTreeView_InsertItem and _GUICtrlTreeView_Delete** with an item handle

Edit: Checked file count returned in @Extended, corrections...

;coded by rover 2k12
#include <File.au3>
#include <GUIConstantsEx.au3>
#include <GuiTreeView.au3>


Main()


Func Main()
Local $aSrcFolder[1] = [0], $aFileList, $aChkFiles, $hItem ; use Local within a function, not Dim
Local $Main_Window = GUICreate("Checkboxes Select With File List", 413, 330, 302, 218)
Local $cBtn = GUICtrlCreateButton("Get Checked Files", 16, 295, 120, 25)
Local $cTreeView = GUICtrlCreateTreeView(16, 16, 377, 272, $TVS_CHECKBOXES)
Local $hTreeView = GUICtrlGetHandle(-1)
GUISetState(@SW_SHOW)

$aFileList = _FileListToArray(@WindowsDir, "*.exe", 1)
If @error = 1 Or @error = 4 Then Exit MsgBox(0, "", "Folders Not Found or No Files Found.")
_AddFolder($cTreeView, $aSrcFolder, $aFileList, @WindowsDir)

$aFileList = _FileListToArray(@SystemDir, "*.exe", 1)
If @error = 1 Or @error = 4 Then Exit MsgBox(0, "", "Folders Not Found or No Files Found.")
_AddFolder($cTreeView, $aSrcFolder, $aFileList, @SystemDir)
$aFileList = 0 ; clear temporary array

While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $cBtn
$aChkFiles = _GetTVCheckedFiles($cTreeView, $aSrcFolder)
If Not @error Then
ConsoleWrite("Files Checked: " & @extended & @LF)
For $i = 0 To UBound($aChkFiles)-1
ConsoleWrite("+ " & $aChkFiles[$i] & @LF)
Next
EndIf
EndSwitch
WEnd
EndFunc ;==>Main



Func _AddFolder($hWnd, ByRef $aSrc, ByRef $aFiles, $sPath)
;coded by rover 2k12
If Not IsHWnd($hWnd) Then $hWnd = GUICtrlGetHandle($hWnd)
If Not IsHWnd($hWnd) Then Return -1
;Add source folder to array and store 1 based index in treeview user data (default value in unused treeview item data is 0, using a 1 based index allows unambiguous treeview userdata validation)
Local $iX = UBound($aSrc), $hItem
If @error Or $iX < 1 Then Return -2 ; not array unless 1 or more
ReDim $aSrc[$iX + 1] ;resize array, add 1 element, now 2 or more
$aSrc[$iX] = $sPath ;ignore first element, fill array element 1 or more with source folder name

For $iIdx = 1 To $aFiles[0]
$POINT = StringInStr($aFiles[$iIdx], ".", 0, -1)
If $POINT <> 0 Then
$hItem = _GUICtrlTreeView_Add($hWnd, 0, $aFiles[$iIdx])
_GUICtrlTreeView_SetItemParam($hWnd, $hItem, $iX) ;store array index
EndIf
Next
Return 1
EndFunc ;==>_AddFolder


Func _GetTVCheckedFiles($hWnd, ByRef $aSrc)
;coded by rover 2k12
If Not IsHWnd($hWnd) Then $hWnd = GUICtrlGetHandle($hWnd)
If Not IsHWnd($hWnd) Then Return SetError(1, 0, -1)
Local $iCnt = _GUICtrlTreeView_GetCount($hWnd), $aRet[$iCnt], $iChk = 0
Local $hItem = _GUICtrlTreeView_GetFirstItem($hWnd)
For $i = 1 To $iCnt
If _GUICtrlTreeView_GetChecked($hWnd, $hItem) Then
$aRet[$iChk] = $aSrc[_GUICtrlTreeView_GetItemParam($hWnd, $hItem)] & "" & _GUICtrlTreeView_GetText($hWnd, $hItem)
$iChk += 1
EndIf
$hItem = _GUICtrlTreeView_GetNextSibling($hWnd, $hItem)
Next
If Not $iChk Then Return SetError(2, 0, -1)
ReDim $aRet[$iChk]
Return SetError(0, $iChk, $aRet)
EndFunc ;==>_GetTVCheckedFiles
Edited by rover

I see fascists...

Link to comment
Share on other sites

Man while every time i make mistake i learned something, :) and yes i know what it is, but hey i am in a learning stage still,

anyways, i found the solution i had to deep study in Arrays stuff , now i am more clear....

only thing i am not getting to Check or Uncheck Items Automatically, by their Windows handle i tried searching here and there but couldnt find possible stuff for learning, as u can see below !

#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GuiComboBox.au3>
#include <WinAPI.au3>
#include <IE.au3>
#include <Date.au3>
#include <array.au3>
#include <GuiTreeView.au3>
#include <GuiListBox.au3>
#include <file.au3>
#include <TreeViewConstants.au3>

$dir1 = @WindowsDir
$FileList1 = _FileListToArray($dir1,"*.exe",1)
If @error = 1 Or @error = 4 Then Exit MsgBox(0, "", "Folders Not Found or No Files Found.")
Global $hItem1[100],$INDEX1


#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Files Check ", 615, 438, 192, 114)
$pattern1 = GUICtrlCreateCheckbox("Check/Uncheck", 72, 16, 137, 33)
GUICtrlSetState(-1, $GUI_UNCHECKED)
$TreeView1 = GUICtrlCreateTreeView(240, 40, 289, 209, $TVS_CHECKBOXES)
GUICtrlSetState(-1, $GUI_DISABLE)
_GUICtrlTreeView_BeginUpdate($TreeView1)
For $INDEX1 = 1 To $FileList1[0]
$POINT1 = StringInStr($FileList1[$INDEX1],".",0,-1)
If $POINT1 <> 0 Then
$hItem1[$INDEX1] = GUICtrlCreateTreeViewItem($FileList1[$INDEX1], $TreeView1)
;GUICtrlSetData(-1, $FILE[$INDEX])
EndIf
Next
_GUICtrlTreeView_EndUpdate($TreeView1)
$run_script = GUICtrlCreateButton("Run Selected Files", 304, 288, 193, 49)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit

case $pattern1
If BitAND(GUICtrlRead($pattern1), $GUI_CHECKED) Then
GUICtrlSetState($TreeView1, $GUI_ENABLE)
Else
GUICtrlSetState($TreeView1, $GUI_DISABLE)
EndIf


Case $run_script
For $INDEX1 = 1 to $FileList1[0]
Local $script_selected1
If _GUICtrlTreeView_GetChecked($TreeView1, $hItem1[$INDEX1]) = True Then
$script_selected1 = _GUICtrlTreeView_GetText($TreeView1, $hItem1[$INDEX1])
Sleep(1000)
RunWait($dir1 & "/" & $script_selected1, $dir1)
Sleep(1000)
EndIf
Next
MsgBox(64,"Complete", "Your Automation Is Completed")


EndSwitch
WEnd

Now as u can see here, i want to Uncheck all items in the Treeview if i Uncheck the "Check/Uncheck" of GUI !

But i am not getting it, Rover thanks for ur help i will apply your way of Logic for my Scripting ...

Rover : "Knowledge has no reach, Explore as much as you can to get success, and sometimes its needed to make promises, so that you can actually achieve something with adrenaline." :) dont take me in a wrong sense :) :) u guyz are great Help

Edited by rockscena
Link to comment
Share on other sites

You are not re-sizing the treeview item array $hItem1.

If your source folder has more than 100 files you will get

a fatal 'Array variable has incorrect number of subscripts or subscript dimension range exceeded.:' message from AU3Check

Set the size of $hItem1 to the file count +1 in element 0 of $FileList1

You can eliminate that array of item handles if you are only getting the checked items

or checking/unchecking all items as in this example.

To check/uncheck all items, use _GUICtrlTreeView_GetFirstItem/_GUICtrlTreeView_GetNextSibling

to loop through all items, and as Zedna pointed out, use _GUICtrlTreeView_GetChecked.

You don't need to declare a variable with For/Next loops - see the help file, just don't use the variable outside of the loop

This should get you going...

#include <File.au3>
#include <GUIConstantsEx.au3>
#include <GuiTreeView.au3>

Local $aSrcFolder[1] = [0]
Local $dir1 = @WindowsDir

#region ### START Koda GUI section ### Form=
Local $Form1 = GUICreate("Files Check ", 615, 438, 192, 114)
Local $pattern1 = GUICtrlCreateCheckbox("Check/Uncheck", 72, 16, 137, 33)
GUICtrlSetState(-1, $GUI_UNCHECKED)
Local $run_script = GUICtrlCreateButton("Run Selected Files", 304, 288, 193, 49)
Local $TreeView1 = GUICtrlCreateTreeView(240, 40, 289, 209, $TVS_CHECKBOXES)
;GUICtrlSetState(-1, $GUI_DISABLE)

Local $FileList1 = _FileListToArray($dir1, "*.exe", 1)
If @error = 1 Or @error = 4 Then Exit MsgBox(0, "", "Folders Not Found or No Files Found.")
_AddFolder($TreeView1, $aSrcFolder, $FileList1, $dir1)


$FileList1 = _FileListToArray(@SystemDir, "*.exe", 1)
If @error = 1 Or @error = 4 Then Exit MsgBox(0, "", "Folders Not Found or No Files Found.")
_AddFolder($TreeView1, $aSrcFolder, $FileList1, @SystemDir)
$FileList1 = 0 ; clear temporary array


GUISetState(@SW_SHOW)
#endregion ### END Koda GUI section ###

While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit

Case $pattern1
If BitAND(GUICtrlRead($pattern1), $GUI_CHECKED) Then
_TVCheckAll($TreeView1)
ConsoleWrite('+Checked Count = ' & @extended & @CRLF )
;GUICtrlSetState($TreeView1, $GUI_ENABLE)
Else
_TVCheckAll($TreeView1, False)
ConsoleWrite('-UnChecked Count = ' & @extended & @CRLF )
;GUICtrlSetState($TreeView1, $GUI_DISABLE)
EndIf

Case $run_script
If _RunCheckedFiles($TreeView1, $aSrcFolder) Then
ConsoleWrite("Files Run: " & @extended & @LF)
MsgBox(64, "Complete", "Your Automation Is Completed")
Else
ConsoleWrite("Files Run: " & @extended & @LF)
MsgBox(64, "Error", "No files selected")
EndIf
EndSwitch
WEnd


Func _RunCheckedFiles($hWnd, ByRef $aSrc)
;coded by rover 2k12
If Not IsHWnd($hWnd) Then $hWnd = GUICtrlGetHandle($hWnd)
If Not IsHWnd($hWnd) Then Return SetError(1, 0, 0)
Local $iChk = 0, $sPath, $sFile
Local $hItem = _GUICtrlTreeView_GetFirstItem($hWnd)
Do
If _GUICtrlTreeView_GetChecked($hWnd, $hItem) Then
$sPath = $aSrc[_GUICtrlTreeView_GetItemParam($hWnd, $hItem)]
$sFile = _GUICtrlTreeView_GetText($hWnd, $hItem)
$iChk += 1
;Sleep(1000)
;RunWait($sPath & "\" & $sFile, $sPath)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $sPath & "\" & $sFile = ' & $sPath & "\" & $sFile & @crlf)
;Sleep(1000)
EndIf
$hItem = _GUICtrlTreeView_GetNextSibling($hWnd, $hItem)
Until $hItem = 0
If Not $iChk Then Return SetError(2, 0, 0) ;no items checked
Return SetError(0, $iChk, 1) ;return number of files run in @Extended
EndFunc ;==>_RunCheckedFiles

Func _AddFolder($hWnd, ByRef $aSrc, ByRef $aFiles, $sPath)
;coded by rover 2k12
If Not IsHWnd($hWnd) Then $hWnd = GUICtrlGetHandle($hWnd)
If Not IsHWnd($hWnd) Then Return -1
;Add source folder to array and store 1 based index in treeview user data (default value in unused treeview item data is 0, using a 1 based index allows unambiguous treeview userdata validation)
Local $iX = UBound($aSrc), $hItem
If @error Or $iX < 1 Then Return -2 ; not array unless 1 or more
ReDim $aSrc[$iX + 1] ;resize array, add 1 element, now 2 or more
$aSrc[$iX] = $sPath ;ignore first element, fill array element 1 or more with source folder name
_GUICtrlTreeView_BeginUpdate($hWnd)
For $iIdx = 1 To $aFiles[0]
If StringInStr($aFiles[$iIdx], ".", 0, -1) <> 0 Then
$hItem = _GUICtrlTreeView_Add($hWnd, 0, $aFiles[$iIdx])
_GUICtrlTreeView_SetItemParam($hWnd, $hItem, $iX) ;store array index
EndIf
Next
_GUICtrlTreeView_EndUpdate($hWnd)
Return 1
EndFunc ;==>_AddFolder

Func _TVCheckAll($hWnd, $fCheck = True)
;coded by rover 2k12
If Not IsHWnd($hWnd) Then $hWnd = GUICtrlGetHandle($hWnd)
If Not IsHWnd($hWnd) Then Return SetError(1, 0, False)
Local $hItem = _GUICtrlTreeView_GetFirstItem($hWnd), $iChk = 0
_GUICtrlTreeView_BeginUpdate($hWnd)
Do
If _GUICtrlTreeView_SetChecked($hWnd, $hItem, $fCheck) Then $iChk += 1
$hItem = _GUICtrlTreeView_GetNextSibling($hWnd, $hItem)
Until $hItem = 0
_GUICtrlTreeView_EndUpdate($hWnd)
Return SetError(0, $iChk, True)
EndFunc ;==>_TVCheckAll

I see fascists...

Link to comment
Share on other sites

  • 2 weeks later...

You are not re-sizing the treeview item array $hItem1.

If your source folder has more than 100 files you will get

a fatal 'Array variable has incorrect number of subscripts or subscript dimension range exceeded.:' message from AU3Check

Set the size of $hItem1 to the file count +1 in element 0 of $FileList1

You can eliminate that array of item handles if you are only getting the checked items

or checking/unchecking all items as in this example.

To check/uncheck all items, use _GUICtrlTreeView_GetFirstItem/_GUICtrlTreeView_GetNextSibling

to loop through all items, and as Zedna pointed out, use _GUICtrlTreeView_GetChecked.

You don't need to declare a variable with For/Next loops - see the help file, just don't use the variable outside of the loop

This should get you going...

#include <File.au3>
#include <GUIConstantsEx.au3>
#include <GuiTreeView.au3>

Local $aSrcFolder[1] = [0]
Local $dir1 = @WindowsDir

#region ### START Koda GUI section ### Form=
Local $Form1 = GUICreate("Files Check ", 615, 438, 192, 114)
Local $pattern1 = GUICtrlCreateCheckbox("Check/Uncheck", 72, 16, 137, 33)
GUICtrlSetState(-1, $GUI_UNCHECKED)
Local $run_script = GUICtrlCreateButton("Run Selected Files", 304, 288, 193, 49)
Local $TreeView1 = GUICtrlCreateTreeView(240, 40, 289, 209, $TVS_CHECKBOXES)
;GUICtrlSetState(-1, $GUI_DISABLE)

Local $FileList1 = _FileListToArray($dir1, "*.exe", 1)
If @error = 1 Or @error = 4 Then Exit MsgBox(0, "", "Folders Not Found or No Files Found.")
_AddFolder($TreeView1, $aSrcFolder, $FileList1, $dir1)


$FileList1 = _FileListToArray(@SystemDir, "*.exe", 1)
If @error = 1 Or @error = 4 Then Exit MsgBox(0, "", "Folders Not Found or No Files Found.")
_AddFolder($TreeView1, $aSrcFolder, $FileList1, @SystemDir)
$FileList1 = 0 ; clear temporary array


GUISetState(@SW_SHOW)
#endregion ### END Koda GUI section ###

While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit

Case $pattern1
If BitAND(GUICtrlRead($pattern1), $GUI_CHECKED) Then
_TVCheckAll($TreeView1)
ConsoleWrite('+Checked Count = ' & @extended & @CRLF )
;GUICtrlSetState($TreeView1, $GUI_ENABLE)
Else
_TVCheckAll($TreeView1, False)
ConsoleWrite('-UnChecked Count = ' & @extended & @CRLF )
;GUICtrlSetState($TreeView1, $GUI_DISABLE)
EndIf

Case $run_script
If _RunCheckedFiles($TreeView1, $aSrcFolder) Then
ConsoleWrite("Files Run: " & @extended & @LF)
MsgBox(64, "Complete", "Your Automation Is Completed")
Else
ConsoleWrite("Files Run: " & @extended & @LF)
MsgBox(64, "Error", "No files selected")
EndIf
EndSwitch
WEnd


Func _RunCheckedFiles($hWnd, ByRef $aSrc)
;coded by rover 2k12
If Not IsHWnd($hWnd) Then $hWnd = GUICtrlGetHandle($hWnd)
If Not IsHWnd($hWnd) Then Return SetError(1, 0, 0)
Local $iChk = 0, $sPath, $sFile
Local $hItem = _GUICtrlTreeView_GetFirstItem($hWnd)
Do
If _GUICtrlTreeView_GetChecked($hWnd, $hItem) Then
$sPath = $aSrc[_GUICtrlTreeView_GetItemParam($hWnd, $hItem)]
$sFile = _GUICtrlTreeView_GetText($hWnd, $hItem)
$iChk += 1
;Sleep(1000)
;RunWait($sPath & "" & $sFile, $sPath)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $sPath & "" & $sFile = ' & $sPath & "" & $sFile & @crlf)
;Sleep(1000)
EndIf
$hItem = _GUICtrlTreeView_GetNextSibling($hWnd, $hItem)
Until $hItem = 0
If Not $iChk Then Return SetError(2, 0, 0) ;no items checked
Return SetError(0, $iChk, 1) ;return number of files run in @Extended
EndFunc ;==>_RunCheckedFiles

Func _AddFolder($hWnd, ByRef $aSrc, ByRef $aFiles, $sPath)
;coded by rover 2k12
If Not IsHWnd($hWnd) Then $hWnd = GUICtrlGetHandle($hWnd)
If Not IsHWnd($hWnd) Then Return -1
;Add source folder to array and store 1 based index in treeview user data (default value in unused treeview item data is 0, using a 1 based index allows unambiguous treeview userdata validation)
Local $iX = UBound($aSrc), $hItem
If @error Or $iX < 1 Then Return -2 ; not array unless 1 or more
ReDim $aSrc[$iX + 1] ;resize array, add 1 element, now 2 or more
$aSrc[$iX] = $sPath ;ignore first element, fill array element 1 or more with source folder name
_GUICtrlTreeView_BeginUpdate($hWnd)
For $iIdx = 1 To $aFiles[0]
If StringInStr($aFiles[$iIdx], ".", 0, -1) <> 0 Then
$hItem = _GUICtrlTreeView_Add($hWnd, 0, $aFiles[$iIdx])
_GUICtrlTreeView_SetItemParam($hWnd, $hItem, $iX) ;store array index
EndIf
Next
_GUICtrlTreeView_EndUpdate($hWnd)
Return 1
EndFunc ;==>_AddFolder

Func _TVCheckAll($hWnd, $fCheck = True)
;coded by rover 2k12
If Not IsHWnd($hWnd) Then $hWnd = GUICtrlGetHandle($hWnd)
If Not IsHWnd($hWnd) Then Return SetError(1, 0, False)
Local $hItem = _GUICtrlTreeView_GetFirstItem($hWnd), $iChk = 0
_GUICtrlTreeView_BeginUpdate($hWnd)
Do
If _GUICtrlTreeView_SetChecked($hWnd, $hItem, $fCheck) Then $iChk += 1
$hItem = _GUICtrlTreeView_GetNextSibling($hWnd, $hItem)
Until $hItem = 0
_GUICtrlTreeView_EndUpdate($hWnd)
Return SetError(0, $iChk, True)
EndFunc ;==>_TVCheckAll

Bro Thanks a Lot, It helped me a lot.

Link to comment
Share on other sites

@OP:

If it is possible to use a listview you can use _GUICtrlListView_SetItemChecked. The function has parameters to check/uncheck all items that have the $LVS_EX_CHECKBOXES extended style.

Just know that if you have a few thousand items to check/uncheck it will take a up to a few minutes (at least in my testing that I could not speed up even using a loop).

Link to comment
Share on other sites

@OP:

If it is possible to use a listview you can use _GUICtrlListView_SetItemChecked. The function has parameters to check/uncheck all items that have the $LVS_EX_CHECKBOXES extended style.

Just know that if you have a few thousand items to check/uncheck it will take a up to a few minutes (at least in my testing that I could not speed up even using a loop).

Yes it has been Solved chk out wat Rover has posted, he is using a Handle for operating multiple Files of diffrent location.

Link to comment
Share on other sites

@OP:

If it is possible to use a listview you can use _GUICtrlListView_SetItemChecked. The function has parameters to check/uncheck all items that have the $LVS_EX_CHECKBOXES extended style.

Just know that if you have a few thousand items to check/uncheck it will take a up to a few minutes (at least in my testing that I could not speed up even using a loop).

@2Reg2Post

The OP is only running selected programs (exe) from the treeview, and most likely not in the thousands.

Using a listview was suggested by me in a previous thread by the OP.

Yes, a listview check all with _GUICtrlListView_SetItemChecked $iIndex = -1 (internal ForNext loop) is faster than a treeview, and 2x faster if _GUICtrlListView_SetItemChecked is optimized.

Unfortunately there is no listview/treeview single message to select all checkboxes without having to loop through all items.

A few thousand treeview items do not take several minutes to check.

Checking 5000 items with _GUICtrlTreeView_SetChecked $iIndex = -1 takes approx 5 seconds, and that can be halved with optimization.

Both the treeview and listview functions can be optimized by using only the listview control ID* and removing the unicode, external process and handle/control ID checks.

*_SendMessage() with a handle is 4 times slower than GuiSendMsg() with a control ID.

;optimized check all

Func _TVCheckAll($hWnd, $fCheck = True)
    ;coded by rover 2k12
    If Not IsHWnd($hWnd) Then $hWnd = GUICtrlGetHandle($hWnd)
    If Not IsHWnd($hWnd) Then Return SetError(1, 0, False)
    Local $hItem = _GUICtrlTreeView_GetFirstItem($hWnd), $iChk = 0
    _GUICtrlTreeView_BeginUpdate($hWnd)
;~     Do
;~         If _GUICtrlTreeView_SetChecked($hWnd, $hItem, $fCheck) Then $iChk += 1
;~         $hItem = _GUICtrlTreeView_GetNextSibling($hWnd, $hItem)
;~     Until $hItem = 0

    Local $tItem = DllStructCreate($tagTVITEMEX)
    DllStructSetData($tItem, "Mask", $TVIF_STATE)
    DllStructSetData($tItem, "StateMask", 0xf000)
    If ($fCheck) Then
        DllStructSetData($tItem, "State", 0x2000)
    Else
        DllStructSetData($tItem, "State", 0x1000)
    EndIf

    Do
        DllStructSetData($tItem, "hItem", $hItem)
        If _SendMessage($hWnd, $TVM_SETITEMW, 0, $tItem, 0, "wparam", "struct*") Then $iChk += 1
        $hItem = _SendMessage($hWnd, $TVM_GETNEXTITEM, $TVGN_NEXT, $hItem, 0, "wparam", "handle", "handle")
    Until $hItem = 0
    _GUICtrlTreeView_EndUpdate($hWnd)
    Return SetError(0, $iChk, True)
EndFunc   ;==>_TVCheckAll

I see fascists...

Link to comment
Share on other sites

@rover:

Good info. Look forward to testing your tuned code later on. The few minutes (~2) to check/uncheck all items (~2,000) in a listview was irking me. Not sure why your system can check/uncheck 5,000 items in 5 sec while my test system with a quad core i7, 16GB DDR3, and RAID0 SSDs cannot. <shrug> Is there a major performance difference between _GUICtrlTreeView_SetChecked and _GUICtrlListView_SetItemChecked? I have only tried with a listview that has thumbnails.

Link to comment
Share on other sites

@rover:

Good info. Look forward to testing your tuned code later on. The few minutes (~2) to check/uncheck all items (~2,000) in a listview was irking me. Not sure why your system can check/uncheck 5,000 items in 5 sec while my test system with a quad core i7, 16GB DDR3, and RAID0 SSDs cannot. <shrug> Is there a major performance difference between _GUICtrlTreeView_SetChecked and _GUICtrlListView_SetItemChecked? I have only tried with a listview that has thumbnails.

Your slowdown would be from the large number of thumbnails.

If you are not already doing so, use the BeginUpdate/EndUpdate functions before and after calling SetItemChecked

and use the extended style LVS_EX_DOUBLEBUFFER. (just a paint buffer that helps with flickering, especially with customdrawn listviews and graphics painting, wont help with your issue)

Maybe move to a virtual (ownerdata) listview - only draws the number of items that fit in the listview client area.

Uses the WM_NOTIFY LVN_GETDISPINFOW notification to update the items shown as the listview is scrolled.

That way you only load thumbnails on demand, and only as many as are visible

or use a cache of images in an array so each full page scroll is not having to load all the images for a page at once.

I think there are examples of this on the forum.

I coded an example loading thumbnails to an array, just to try it, but the memory use was high.

This optimized version of _GUICtrlListView_SetItemChecked halves the time to check all items.

Modified for listview control ID only

The example is slower creating the items with the unmodified udf _GUICtrlListView_AddItem/AddSubItem functions than it is with the native GUICtrlCreateListViewItem

Edit: added Begin/EndUpdate functions

#include <GUIConstantsEx.au3>
#include <GuiListView.au3>

_Main()

Func _Main()
Local $exStyles = BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_CHECKBOXES), $cListView

GUICreate("ListView Set Item Checked State", 400, 300)
$cListView = GUICtrlCreateListView("", 2, 2, 394, 268)
_GUICtrlListView_SetExtendedListViewStyle($cListView, $exStyles)
GUISetState()

_GUICtrlListView_BeginUpdate($cListView)
_GUICtrlListView_AddColumn($cListView, "Column 1", 100)
_GUICtrlListView_AddColumn($cListView, "Column 2", 100)
_GUICtrlListView_AddColumn($cListView, "Column 3", 100)
Local $iIdx
ConsoleWrite("+ Creating 5000 items " & @LF)
; Add items
For $i = 0 To 5000
$iIdx = _GUICtrlListView_AddItem($cListView, "Row 1: Col 1", 0)
_GUICtrlListView_AddSubItem($cListView, $iIdx, "Row 1: Col 2", 1)
_GUICtrlListView_AddSubItem($cListView, $iIdx, "Row 1: Col 3", 2)
Next
_GUICtrlListView_EndUpdate($cListView)

_GUICtrlListView_BeginUpdate($cListView)
ConsoleWrite("+ Check all items " & @LF)
Local $iT = TimerInit()
__GUICtrlListView_SetItemChecked($cListView, -1, True)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : TimerDiff($iT) = ' & TimerDiff($iT) & @crlf)
_GUICtrlListView_EndUpdate($cListView)
Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE
GUIDelete()
EndFunc ;==>_Main


;modified for GUICtrlCreateListView() control ID only
Func __GUICtrlListView_SetItemChecked($cLV, $iIndex, $fCheck = True)
Local $pItem, $tItem, $iState = 0x1000
$tItem = DllStructCreate($tagLVITEM)
$pItem = DllStructGetPtr($tItem)
DllStructSetData($tItem, "Mask", $LVIF_STATE)
DllStructSetData($tItem, "StateMask", $LVIS_STATEIMAGEMASK)
If @error Then Return SetError($LV_ERR, $LV_ERR, $LV_ERR)
If ($fCheck) Then $iState = 0x2000
Local $hLV = GUICtrlGetHandle($cLV)
If $iIndex <> -1 Then ;check single item
DllStructSetData($tItem, "Item", $iIndex)
DllStructSetData($tItem, "State", $iState)
Return GUICtrlSendMsg($cLV, $LVM_SETITEMW, 0, $pItem) <> 0
Else ;check all
For $x = 0 To _GUICtrlListView_GetItemCount($cLV) - 1
DllStructSetData($tItem, "Item", $x)
DllStructSetData($tItem, "State", $iState)
;_SendMessage($cLV, $LVM_SETITEMW, 0, $tItem, 0, "wparam", "struct*")
If Not GUICtrlSendMsg($cLV, $LVM_SETITEMW, 0, $pItem) <> 0 Then
Return SetError($LV_ERR, $LV_ERR, $LV_ERR)
EndIf
Next
Return True
EndIf
Return False
EndFunc ;==>__GUICtrlListView_SetItemChecked
Edited by rover

I see fascists...

Link to comment
Share on other sites

@rover:

Is there a major performance difference between _GUICtrlTreeView_SetChecked and _GUICtrlListView_SetItemChecked? I have only tried with a listview that has thumbnails.

_GUICtrlListView_SetItemChecked is faster than _GUICtrlTreeView_SetChecked, even after both are optimized.

I see fascists...

Link to comment
Share on other sites

Your slowdown would be from the large number of thumbnails.

Maybe move to a virtual (ownerdata) listview - only draws the number of items that fit in the listview client area.

Uses the WM_NOTIFY LVN_GETDISPINFOW notification to update the items shown as the listview is scrolled.

That way you only load thumbnails on demand, and only as many as are visible

or use a cache of images in an array so each full page scroll is not having to load all the images for a page at once.

Excellent suggestions based on insight that were previously unknown to me. Thank you for your extended responses. :thumbsup:

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...