Date: 02-16-2022
Return to Index
created by gbSnippets
'Compilable Example: (Jose Includes)
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
#Include "CommCtrl.inc"
%IDC_TreeView = 500 : %IDC_ListBox = 501 : %IDC_TextBox = 502
%IDC_Sort = 600 : %IDC_From = 601 : %IDC_To = 602
Global hDlg As Dword
Function PBMain() As Long
Local hItem,hFont As Dword, style&
Font New "Consolas", 8, 0 To hFont
style& = %WS_Child Or %WS_Visible Or %ES_MultiLine Or %WS_VScroll Or %ES_AutoHScroll _
Or %WS_HScroll Or %ES_AutoVScroll Or %ES_WantReturn Or %ES_NoHideSel Or %WS_TabStop
Dialog New Pixels, 0, "File/Folder Browser",300,300,550,330, %WS_OverlappedWindow To hDlg
Control Add Button, hDlg, %IDC_Sort, "Sort", 370,15,50,20
Control Add TextBox, hDlg, %IDC_From, "1", 430, 15, 50, 20
Control Add TextBox, hDlg, %IDC_To, "10", 490, 15, 50, 20
Control Add ListBox, hDlg, %IDC_ListBox, , 190, 15, 170, 310
Control Add TextBox, hDlg, %IDC_TextBox, "", 370, 45, 170, 270, style&
Control Set Font hDlg, %IDC_TextBox, hFont
Control Add Treeview, hDlg, %IDC_TreeView, "TopLeft", 10, 15, 170, 300
Treeview Insert Item hDlg, %IDC_TreeView, 0, %TVI_Last, 1,1,"c:\" To hItem
Treeview Insert Item hDlg, %IDC_TreeView, hItem, %TVI_Last, 1,4,"n/a" To hItem
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Local temp$, hNode As Dword, iFrom, iTo As Long
Dim D() As String
Select Case Cb.Msg
Case %WM_InitDialog
Treeview Get Root hDlg, %IDC_TreeView To hNode
DisplayChildren hNode
Treeview Set Expanded hDlg, %IDC_TreeView, hNode, %True
Case %WM_Command
Select Case Cb.Ctl
Case %IDC_Sort
Control Get Text hDlg, %IDC_From To temp$ : iFrom = Val(temp$)
Control Get Text hDlg, %IDC_To To temp$ : iTo = Val(temp$)
If iFrom <= 0 Then iFrom = 1 : If iTo <= 0 Then iTo = 10
Control Get Text hDlg, %IDC_TextBox To temp$
ReDim D(ParseCount(temp$,$CrLf)-1)
Parse temp$,D(), $CrLf
Array Sort D(), From iFrom To iTo, Collate UCase, Ascend
Control Set Text hDlg, %IDC_TextBox, Join$(D(),$CrLf)
Case %IDC_ListBox
Select Case Cb.CtlMsg
Case %LBN_SelChange
ListBox Get Text hDlg, %IDC_ListBox To temp$
Treeview Get Select hDlg, %IDC_TreeView To hNode
temp$ = FullTreePath(hNode) + "\" + temp$
Replace "\\" With "\" In temp$
If InStr(LCase$(temp$),".txt") Or InStr(LCase$(temp$),".bas") Then
If IsFile(temp$) Then
Open temp$ For Binary As #1 : Get$ #1, Lof(1), temp$ : Close #1
Control Set Text hDlg, %IDC_TextBox, temp$
End If
End If
End Select
End Select
Case %WM_Notify
Select Case Cb.NmId
Case %IDC_TreeView
Select Case Cb.NmCode
Case %TVN_ItemExpanding
Local N As NM_TreeView Ptr
N = Cb.LParam
DisplayChildren @N.ItemNew.hItem
Case %TVN_SelChanged
DisplayFiles
End Select
End Select
End Select
End Function
Sub DisplayChildren (hParent As Dword)
Local hChild, hTemp As Dword
Local ChildDIR, ParentDIR, temp As String
Local FullPath As String
'get text of parent node that is is expanding
Treeview Get Text hDlg, %IDC_TreeView, hParent To ParentDIR
ParentDIR = Trim$(ParentDIR, "\") 'this turns c:\ into c:
'remove all children to ensure currency of display and directory structure
Do
Treeview Get Child hDlg, %IDC_TreeView, hParent To hChild
If hChild Then Treeview Delete hDlg, %IDC_TreeView, hChild
Loop While hChild
'add nodes for each subfolder
ChildDir = Dir$(FullTreePath(hParent) + "\*.*", Only %SubDir)
While Len(ChildDIR)
Treeview Insert Item hDlg, %IDC_TreeView, hParent, %TVI_Last, 0, 0, ChildDIR To hTemp
ChildDIR = Dir$
Wend
'add dummy child to each tree element whose folder has subfolders (causes TreeView to display + sign)
Treeview Get Child hDlg, %IDC_TreeView, hParent To hChild
While hChild
FullPath = FullTreePath(hChild)
temp = Dir$(FullPath + "\*.*", Only %SubDir)
If Len(temp) Then Treeview Insert Item hDlg, %IDC_TreeView, hChild, %TVI_Last, 0, 0, "n/a" To hTemp
Treeview Get Next hDlg, %IDC_TreeView, hChild To hChild
Wend
End Sub
Function FullTreePath(ByVal hNode As Dword) As String
'get full directory path for hNode
Local hRoot As Dword
Local FullPath, temp As String
Do
Treeview Get Text hDlg, %IDC_TreeView, hNode To temp
FullPath = "\" + temp + FullPath
Treeview Get Parent hDlg, %IDC_TreeView, hNode To hNode
Loop Until hNode = 0
Function = LTrim$(FullPath, "\")
End Function
Sub DisplayFiles
Local temp As String
Local hNode As Dword
ListBox Reset hDlg, %IDC_ListBox
ListBox Add hDlg, %IDC_ListBox, "<no files>"
Treeview Get Select hDlg, %IDC_Treeview To hNode
temp = Dir$(FullTreePath(hNode)+"\*.*")
If Len(temp) Then ListBox Reset hDlg, %IDC_ListBox
Do
If Len(temp) Then ListBox Add hDlg, %IDC_ListBox, temp
temp = Dir$
Loop While Len(temp)
End Sub
'gbs_00736
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm