This problem is a rather tricky one. For an ExplorerTreeGadget the function GetGadgetText() returns only the currently highlighted folder name. If you expand or collapse a folder by clicking on [+] or [-], GetGadgetText() doesn't report this action. Instead it always returns the currently highlighted directory path or an empty string. I therefore programmed a callback that detects the expand or collapse action and highlights the folder name item behind the [+] or [-]. (If you want to remove this highlighting afterwards you can remove the comment before the SendMessage_() line.) The callback writes the current directory path into the Shared variable CurrentDirPath, so that SetGadgetText(#ExplorerTree, CurrentDirPath + "*.*") can always refresh the last highlighted folder. If the last action was a left click on a directory thus highlighting it, this directory path is used.
The example program creates the empty file Empty.Txt in the currently highlighted directory after a click on the Create File button and refreshes its contents, so that you can see the newly created file:
Code: Select all
Enumeration
#ExplorerTree
#Button
EndEnumeration
Procedure ExplorerTreeCallback(hWnd, Msg, wParam, lParam)
Shared CurrentDirPath.S
If Msg = #WM_NOTIFY
*NMH.NMHDR = lParam
*NMTV.NM_TREEVIEW = lParam
If *NMH\hwndFrom = GadgetID(#ExplorerTree)
If *NMH\code = #TVN_ITEMEXPANDING
If *NMTV\action = #TVE_COLLAPSE
SendMessage_(GadgetID(#ExplorerTree), #TVM_SELECTITEM, #TVGN_CARET, *NMTV\itemNew\hItem)
CurrentDirPath = GetGadgetText(#ExplorerTree)
Debug CurrentDirPath
; SendMessage_(GadgetID(#ExplorerTree), #TVM_SELECTITEM, #TVGN_CARET, 0)
ElseIf *NMTV\action = #TVE_EXPAND
SendMessage_(GadgetID(#ExplorerTree), #TVM_SELECTITEM, #TVGN_CARET, *NMTV\itemNew\hItem)
CurrentDirPath = GetGadgetText(#ExplorerTree)
; SendMessage_(GadgetID(#ExplorerTree), #TVM_SELECTITEM, #TVGN_CARET, 0)
Debug CurrentDirPath
EndIf
ElseIf *NMH\code = #TVN_SELCHANGED
CurrentDirPath = GetGadgetText(#ExplorerTree)
Debug CurrentDirPath
EndIf
EndIf
EndIf
ProcedureReturn #PB_ProcessPureBasicEvents
EndProcedure
OpenWindow(1, 0, 0, 290, 300, "Refresh ExplorerTreeGadget", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
CreateGadgetList(WindowID(1))
ExplorerTreeGadget(#ExplorerTree, 10, 10, 270, 230, "")
ButtonGadget(#Button, 45, 250, 200, 40, "Create new file 'Empty.Txt' in current highlighted Dir and refresh ExplorerTree", #PB_Button_MultiLine | #PB_Button_Default)
SetWindowCallback(@ExplorerTreeCallback())
Repeat
WindowEvent = WaitWindowEvent()
If CurrentDirPath = ""
If ButtonDisabled = #False
DisableGadget(#Button, #True)
ButtonDisabled = #True
EndIf
Else
If ButtonDisabled = #True
DisableGadget(#Button, #False)
ButtonDisabled = #False
EndIf
EndIf
If WindowEvent = #PB_Event_Gadget And EventGadget() = #Button
If CreateFile(1, CurrentDirPath + "Empty.Txt") = 0
MessageRequester("Error", "Creation of " + CurrentDirPath + "'Empty.Txt' failed!", #MB_ICONERROR)
End
EndIf
CloseFile(1)
SetGadgetText(#ExplorerTree, CurrentDirPath + "*.*")
Debug "ExplorerTree refreshed"
EndIf
Until WindowEvent = #PB_Event_CloseWindow