﻿id	summary	reporter	owner	description	type	status	milestone	component	version	severity	resolution	keywords	cc
4039	Bug with _GUICtrlTreeView_Delete	Kanashius	Jpm	"The _GUICtrlTreeView_Delete deletes random controls instead of the provided item using x64 AutoIt.

The issue already came up and was fixed, if a ControlID was provided, but it was overlooked to fix it for ControlHandles, too.

Comment in the UDF to that:
{{{
; Danyfirex founding
; If We Use IsHWnd It will always return false. So You will always call _GUICtrlTreeView_GetItemHandle and You will get the GUICtrlGetHandle issue that happens randomly
; So let's use IsPtr and remove GUICtrlDelete So we can always delete using the Control Handle.
}}}

As it already came up as a Bug, I did not write an additional Example for that.

Proposed fix to change
{{{
; #FUNCTION# ====================================================================================================================
; Author ........: Gary Frost (gafrost)
; Modified.......: re-written by Holger Kotsch, re-written again by Gary Frost
;                  $hItem = Null by Jpm
; ===============================================================================================================================
Func _GUICtrlTreeView_Delete($hWnd, $hItem = Null)
	If IsHWnd($hWnd) Then
		If $hItem = Null Then
			$hItem = _SendMessage($hWnd, $TVM_GETNEXTITEM, $TVGN_CARET, 0, 0, ""wparam"", ""handle"", ""handle"")
			If $hItem <> 0x00000000 Then Return _SendMessage($hWnd, $TVM_DELETEITEM, 0, $hItem, 0, ""wparam"", ""handle"", ""hwnd"") <> 0
			Return False
		Else
			If Not IsHWnd($hItem) Then $hItem = _GUICtrlTreeView_GetItemHandle($hWnd, $hItem)
			If GUICtrlDelete($hItem) Then Return True
			Return _SendMessage($hWnd, $TVM_DELETEITEM, 0, $hItem, 0, ""wparam"", ""handle"", ""hwnd"") <> 0
		EndIf
	Else
		If $hItem = Null Then
			$hItem = GUICtrlSendMsg($hWnd, $TVM_GETNEXTITEM, $TVGN_CARET, 0)
			If $hItem <> 0x00000000 Then Return GUICtrlSendMsg($hWnd, $TVM_DELETEITEM, 0, $hItem) <> 0
			Return False
		Else
			; Danyfirex founding
			; If We Use IsHWnd It will always return false. So You will always call _GUICtrlTreeView_GetItemHandle and You will get the GUICtrlGetHandle issue that happens randomly
			; So let's use IsPtr and remove GUICtrlDelete So we can always delete using the Control Handle.
			If Not IsPtr($hItem) Then
				$hItem = _GUICtrlTreeView_GetItemHandle($hWnd, $hItem)
			Else
				SetExtended(1)
			EndIf
			Return GUICtrlSendMsg($hWnd, $TVM_DELETEITEM, 0, $hItem) <> 0
		EndIf
	EndIf
EndFunc   ;==>_GUICtrlTreeView_Delete
}}}

into

{{{
; #FUNCTION# ====================================================================================================================
; Author ........: Gary Frost (gafrost)
; Modified.......: re-written by Holger Kotsch, re-written again by Gary Frost
;                  $hItem = Null by Jpm
; ===============================================================================================================================
Func _GUICtrlTreeView_Delete($hWnd, $hItem = Null)
	If IsHWnd($hWnd) Then
		If $hItem = Null Then
			$hItem = _SendMessage($hWnd, $TVM_GETNEXTITEM, $TVGN_CARET, 0, 0, ""wparam"", ""handle"", ""handle"")
			If $hItem <> 0x00000000 Then Return _SendMessage($hWnd, $TVM_DELETEITEM, 0, $hItem, 0, ""wparam"", ""handle"", ""hwnd"") <> 0
			Return False
		Else
			If Not IsPtr($hItem) Then 
				$hItem = _GUICtrlTreeView_GetItemHandle($hWnd, $hItem)
			EndIf
			Return _SendMessage($hWnd, $TVM_DELETEITEM, 0, $hItem, 0, ""wparam"", ""handle"", ""hwnd"") <> 0
		EndIf
	Else
		If $hItem = Null Then
			$hItem = GUICtrlSendMsg($hWnd, $TVM_GETNEXTITEM, $TVGN_CARET, 0)
			If $hItem <> 0x00000000 Then Return GUICtrlSendMsg($hWnd, $TVM_DELETEITEM, 0, $hItem) <> 0
			Return False
		Else
			; Danyfirex founding
			; If We Use IsHWnd It will always return false. So You will always call _GUICtrlTreeView_GetItemHandle and You will get the GUICtrlGetHandle issue that happens randomly
			; So let's use IsPtr and remove GUICtrlDelete So we can always delete using the Control Handle.
			If Not IsPtr($hItem) Then
				$hItem = _GUICtrlTreeView_GetItemHandle($hWnd, $hItem)
			Else
				SetExtended(1)
			EndIf
			Return GUICtrlSendMsg($hWnd, $TVM_DELETEITEM, 0, $hItem) <> 0
		EndIf
	EndIf
EndFunc   ;==>_GUICtrlTreeView_Delete
}}}

(Changed lines: 13,14,15(,16))

The problem came up in https://www.autoitscript.com/forum/topic/173926-fileexplorer-treeview-and-listview-treelistexplorer/page/3/#findComment-1541943
"	Bug	closed	3.3.17.0	Standard UDFs	3.3.16.1	None	Fixed	TreeView, _GUICtrlTreeView_Delete	
