Date: 02-16-2022
Return to Index
created by gbSnippets
'Primary Code:
#Compiler PBWin 9, PBWin 10
#Compile Exe
#Dim All
%Unicode=1
#Include "Win32API.inc"
#Include "commctrl.inc"
%IDC_TreeView = 100
%IDC_Label = 200
%IDC_CheckBox = 300
Global hDlg As Dword, AllowedChars$, SearchTerm As AsciiZ * 256
Function PBMain() As Long
Local hItem As Dword, hTemp As Dword, hTemp2 As Dword, hTemp3 As Dword
AllowedChars$ = Chr$(48 To 57, 65 To 90, 97 To 122) '0-9, A-Z, a-z
Dialog New Pixels, 0, "TreeView",,,155,220, %WS_OverlappedWindow, 0 To hDlg
Control Add CheckBox, hDlg, %IDC_CheckBox, "Start at top", 10,10,100,20
Control Add Treeview, hDlg, %IDC_TreeView, "", 10,40,130,200, %TVS_EditLabels
Treeview Insert Item hDlg, %IDC_TreeView, 0, %TVI_Last, 0,0,"Root" To hItem
Treeview Insert Item hDlg, %IDC_TreeView, hItem, %TVI_Last, 0,0,"Mother" To hTemp
Treeview Insert Item hDlg, %IDC_TreeView, hItem, %TVI_Last, 0,0,"Father" To hTemp
Treeview Insert Item hDlg, %IDC_TreeView, hItem, %TVI_Last, 0,0,"Hothead" To hTemp
Treeview Insert Item hDlg, %IDC_TreeView, hItem, %TVI_Last, 0,0,"Cow" To hTemp
Treeview Insert Item hDlg, %IDC_TreeView, hItem, %TVI_Last, 0,0,"Sister" To hTemp
Treeview Insert Item hDlg, %IDC_TreeView, hItem, %TVI_Last, 0,0,"Hotel" To hTemp
Treeview Insert Item hDlg, %IDC_TreeView, hItem, %TVI_Last, 0,0,"Coward" To hTemp
Treeview Set Expanded hDlg, %IDC_TreeView, hItem, %True
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Local pNMTVK As NMTVKEYDOWN Ptr, NewChar$, hNode As Dword
Select Case Cb.Msg
Case %WM_InitDialog
Treeview Get Root hDlg, %IDC_Treeview To hNode
Treeview Get Child hDlg, %IDC_TreeView, hNode To hNode
Treeview Select hDlg, %IDC_TreeView, hNode
Control Set Focus hDlg, %IDC_TreeView
Case %WM_Notify
Select Case Cb.NmId
Case %IDC_TreeView
Select Case Cb.NmCode
Case %TVN_KeyDown
pNMTVK = Cb.LParam
NewChar$ = Chr$(@pNMTVK.wVKey)
If Len(SearchTerm) And (@pNMTVK.wVKey = %VK_Back) Then
NewChar$ = Right$(SearchTerm,1)
SearchTerm = Left$(SearchTerm, Len(SearchTerm)-1)
End If
If InStr(AllowedChars$, NewChar$) Then
SearchTerm = SearchTerm + NewChar$
SearchTree '0=1st sibling of parent 1=1st sibling of selection
Else
SearchTerm = ""
End If
Dialog Set Text hDlg, SearchTerm
Function = %True
End Select
End Select
End Select
End Function
Function SearchTree() As Long '0=1st sibling of parent 1=1st sibling of selection
Local CurrentNode, ParentNode, hNode As Dword, temp$, MatchFound, StartAtTop As Long
Control Get Check hDlg, %IDC_CheckBox To StartAtTop
Treeview Get Select hDlg, %IDC_TreeView To CurrentNode
If StartAtTop Then
'search starts with 1st child of parent
Treeview Get Parent hDlg, %IDC_TreeView, CurrentNode To ParentNode
Treeview Get Child hDlg, %IDC_TreeVIew, ParentNode To hNode
Else
'search starts with next sibling of current node
Treeview Get Next hDlg, %IDC_TreeView, CurrentNode To hNode
End If
While hNode
Treeview Get Text hDlg, %IDC_TreeView, hNode To temp$
If Left$(LCase$(temp$), Len(SearchTerm)) = LCase$(SearchTerm) Then
MatchFound = 1
Treeview Select hDlg, %IDC_TreeView, hNode
Exit Loop
End If
Treeview Get Next hDlg, %IDC_TreeView, hNode To hNode
Wend
If MatchFound And hNode Then
Treeview Select hDlg, %IDC_TreeView, hNode 'match found
Else
Treeview Unselect hDlg, %IDC_TreeView
Beep
End If
End Function
'Compilable Example: (Jose Includes)
#Compiler PBWin 9, PBWin 10
#Compile Exe
#Dim All
%Unicode=1
#Include "Win32API.inc"
#Include "commctrl.inc"
#Include "default_styles.inc"
%IDC_TreeView = 100
%IDC_Label = 200
%IDC_CheckBox = 300
Global hDlg As Dword, AllowedChars$, SearchTerm As AsciiZ * 256
Function PBMain() As Long
Local hRoot, hTemp As Dword
AllowedChars$ = Chr$(48 To 57, 65 To 90, 97 To 122) '0-9, A-Z, a-z
Dialog New Pixels, 0, "TreeView",,,155,220, %WS_OverlappedWindow, 0 To hDlg
Control Add Treeview, hDlg, %IDC_TreeView, "", 10,10,130,200, %tv_ds Or %TVS_EditLabels, %tv_de
Treeview Insert Item hDlg, %IDC_TreeView, 0, %TVI_Last, 0,0,"Root" To hRoot
Treeview Insert Item hDlg, %IDC_TreeView, hRoot, %TVI_Last, 0,0,"Mother" To hTemp
Treeview Insert Item hDlg, %IDC_TreeView, hRoot, %TVI_Last, 0,0,"Dan" To hTemp
Treeview Insert Item hDlg, %IDC_TreeView, hRoot, %TVI_Last, 0,0,"Sam" To hTemp
Treeview Insert Item hDlg, %IDC_TreeView, hRoot, %TVI_Last, 0,0,"Samuel" To hTemp
Treeview Insert Item hDlg, %IDC_TreeView, hRoot, %TVI_Last, 0,0,"Daniel" To hTemp
Treeview Insert Item hDlg, %IDC_TreeView, hRoot, %TVI_Last, 0,0,"Dana" To hTemp
Treeview Set Expanded hDlg, %IDC_TreeView, hRoot, %True
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Local pNMTVK As NMTVKEYDOWN Ptr, NewChar$, hNode As Dword
Select Case Cb.Msg
Case %WM_InitDialog
Treeview Get Root hDlg, %IDC_Treeview To hNode
Treeview Get Child hDlg, %IDC_TreeView, hNode To hNode
Treeview Select hDlg, %IDC_TreeView, hNode
Control Set Focus hDlg, %IDC_TreeView
Case %WM_Notify
Select Case Cb.NmId
Case %IDC_TreeView
Select Case Cb.NmCode
Case %TVN_KeyDown
'get key pressed by user
pNMTVK = Cb.LParam
NewChar$ = Chr$(@pNMTVK.wVKey)
'search only if allowed character received
If InStr(AllowedChars$, NewChar$) Then
SearchTerm = SearchTerm + NewChar$
SearchTree
Else
SearchTerm = ""
End If
Dialog Set Text hDlg, SearchTerm
Function = %True
End Select
End Select
End Select
End Function
Function SearchTree() As Long
Local CurrentNode, ParentNode, hNode As Dword, temp$, MatchFound, StartAtSelection As Long
Treeview Get Select hDlg, %IDC_TreeView To CurrentNode
Treeview Get Parent hDlg, %IDC_TreeView, CurrentNode To ParentNode
Treeview Get Child hDlg, %IDC_TreeVIew, ParentNode To hNode
While hNode
Treeview Get Text hDlg, %IDC_TreeView, hNode To temp$
If Left$(LCase$(temp$), Len(SearchTerm)) = LCase$(SearchTerm) Then
MatchFound = 1
Treeview Select hDlg, %IDC_TreeView, hNode
Exit Loop
End If
Treeview Get Next hDlg, %IDC_TreeView, hNode To hNode
Wend
If MatchFound And hNode Then
Treeview Select hDlg, %IDC_TreeView, hNode 'match found
Else
Treeview Unselect hDlg, %IDC_TreeView
Beep
End If
End Function
'gbs_01168
'Date: 03-25-2
http://www.garybeene.com/sw/gbsnippets.htm