Date: 02-16-2022
Return to Index
created by gbSnippets
'The inplace editing of a TreeView node is supported by the control, but still requires
'a fair amount of code to get it working. Under command of an application, an edit control
'is placed directly over the node being edited. For this to work, the TreeView control must
'use the style %TVS_EditLabels.
'Primary Code:
'Here are the basic steps:
'1. Set Style& of TreeView to include %TVS_EditLabels
Style& = %TVS_HasLines Or %TVS_HasButtons Or %TVS_LinesAtRoot Or %TVS_EditLabels
'2. Create a menu item that will call the %TVM_EditLabel message.
Sub AddMenu()
Menu New Bar To hMenu
Menu New Popup To hMenuEdit
Menu Add Popup, hMenu, "&Edit", hMenuEdit, %MF_Enabled
Menu Add String, hMenuEdit, "&Edit Tree Node" + $Tab + "F2", %IDM_EditTree, %MF_Enabled
Menu Add String, hMenuEdit, "&End Edit of Tree Node", %IDM_EndEditTree, %MF_Enabled
Menu Attach hMenu, hDlg
End Sub
'3. Under %WM_Command, capture selection of the menu and to capture pressing "Enter"
'in the TreeView edit control
Select Case CB.Msg
Case %WM_Command
Select Case CB.Ctl
Case %IDM_EditTree
TreeView Get Select hDlg, %IDC_TreeView To hNode
Control Send hDlg, %IDC_TreeView, %TVM_EditLabel, 0, hNode
Case %IDM_EndEditTree
Control Send hDlg, %IDC_TreeView, %TVM_EndEditLabelNow, %False, 0
Case %IdOk 'pressing Enter in TreeView
If CB.Ctlmsg = %BN_Clicked Then
Select Case GetParent(GetFocus)
Case hTree
Control Send hDlg, %IDC_TreeView, %TVM_EndEditLabelNow, %False, 0
End Select
Function = 1
End If
End Select
'4. Create an accelerator table defining F2 as the EditLabel accelerator key:
Sub BuildAcceleratorTable
Local c As Long, ac() As ACCELAPI, hAccelerator As Dword ' for keyboard accelator table values
Dim ac(0)
ac(0).fvirt = %FVIRTKEY
ac(0).key = %VK_F2
ac(0).cmd = %IDM_EditTree
Accel Attach hDlg, AC() To hAccelerator
End Sub
'5. Use the %WM_Command (%IDOK) to capture the "Enter" key from the
'inplace edit box, which is placed there by Windows in response to %TVM_EditLabel
Case %IdOk 'pressing Enter in TreeView or ComboBox edit box
If CB.Ctlmsg = %BN_Clicked Then
Select Case GetParent(GetFocus)
Case hTree
Control Send hDlg, %IDC_TreeView, %TVM_EndEditLabel, %False, 0
End Select
Function = 1
End If
'6. Use the %WM_Notify to respond to the BeginLabelEdit and EndLabelEdit messages
Case %WM_Notify
Select Case CB.NmID
Case %IDC_TreeView
Select Case CB.Nmcode
Case %TVN_BeginLabelEdit
hTVEdit = Treeview_GetEditControl(hTree) 'handle of TreeView edit control
Control Get Text hTree, GetDlgCtrlID(hTVEdit) To temp$ 'get original text
' Control Send hTree, GetDlgCtrlID(hTVEdit), %EM_LIMITTEXT, 25, 0 '25 char max
Case %TVN_EndLabelEdit
hTVEdit = Treeview_GetEditControl(hTree) 'handle of TreeView edit control
Control Get Text hTree, GetDlgCtrlID(hTVEdit) To temp$ 'get new text
TreeView Get Select hDlg, %IDC_TreeView To hNode
TreeView Set Text hDlg, %IDC_TreeView, hNode, temp$
Function = 1 '1=new text 0=original text
' Control Set Focus hDlg, %IDC_TreeView
End Select
'Then to start editing a TreeView node do any of the following:
'1. triple click the TreeView
'2. click on the menu "Edit Label" menu item
'3. Press F2
'To stop editing, press Enter. Or, click on another node.
'Compilable Example: (Jose Includes)
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
#Include "CommCtrl.inc"
%IDC_TreeView = 100 : %IDM_EditTree = 300 : %IDM_EndEditTree = 400
Global hDlg As Dword, hTree As Dword, hMenu As Dword
Global hMenuEdit As Dword, hTVEdit As Dword
Function PBMain() As Long
Local hItem As Dword, hTemp As Dword, hTemp2 As Dword, hTemp3 As Dword, Style&
Dialog New Pixels, 0, "TreeView",200,200,155,250, %WS_SysMenu, 0 To hDlg
Style& = %TVS_HasLines Or %TVS_HasButtons Or %TVS_LinesAtRoot Or %TVS_EditLabels
Control Add TreeView, hDlg, %IDC_TreeView, "", 10,10,130,200, Style&
Control Handle hDlg, %IDC_TreeView To hTree
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, hTemp, %TVI_Last, 0,0,"Dan" To hTemp2
TreeView Insert Item hDlg, %IDC_TreeView, hTemp, %TVI_Last, 0,0,"Bob" To hTemp3
TreeView Set Expanded hDlg, %IDC_TreeView, hTemp, %True
TreeView Insert Item hDlg, %IDC_TreeView, hTemp3, %TVI_Last, 0,0,"Foot" To hTemp2
TreeView Insert Item hDlg, %IDC_TreeView, hTemp3, %TVI_Last, 0,0,"Arm" To hTemp2
TreeView Set Expanded hDlg, %IDC_TreeView, hTemp3, %True
TreeView Insert Item hDlg, %IDC_TreeView, hItem, %TVI_Last, 0,0,"Father" To hTemp
TreeView Insert Item hDlg, %IDC_TreeView, hTemp, %TVI_Last, 0,0,"Helen" To hTemp2
TreeView Insert Item hDlg, %IDC_TreeView, hTemp, %TVI_Last, 0,0,"Any" To hTemp3
TreeView Set Expanded hDlg, %IDC_TreeView, hTemp, %True
TreeView Insert Item hDlg, %IDC_TreeView, hTemp3, %TVI_Last, 0,0,"Leg" To hTemp2
TreeView Insert Item hDlg, %IDC_TreeView, hTemp3, %TVI_Last, 0,0,"Finger" To hTemp2
TreeView Set Expanded hDlg, %IDC_TreeView, hTemp3, %True
TreeView Set Expanded hDlg, %IDC_TreeView, hItem, %True
AddMenu
BuildAcceleratorTable
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Local hNode As Dword, iResult&, temp$
Select Case CB.Msg
Case %WM_Command
Select Case CB.Ctl
Case %IDM_EditTree
TreeView Get Select hDlg, %IDC_TreeView To hNode
Control Send hDlg, %IDC_TreeView, %TVM_EditLabel, 0, hNode
Case %IDM_EndEditTree
Control Send hDlg, %IDC_TreeView, %TVM_EndEditLabelNow, %False, 0
Case %IdOk 'pressing Enter in TreeView
If CB.Ctlmsg = %BN_Clicked Then
Select Case GetParent(GetFocus)
Case hTree
Control Send hDlg, %IDC_TreeView, %TVM_EndEditLabelNow, %False, 0
End Select
Function = 1
End If
End Select
Case %WM_Notify
Select Case CB.NmID
Case %IDC_TreeView
Select Case CB.Nmcode
Case %TVN_BeginLabelEdit
hTVEdit = Treeview_GetEditControl(hTree) 'handle of TreeView edit control
Control Get Text hTree, GetDlgCtrlID(hTVEdit) To temp$ 'get original text
' Control Send hTree, GetDlgCtrlID(hTVEdit), %EM_LIMITTEXT, 25, 0 '25 char max
Case %TVN_EndLabelEdit
hTVEdit = Treeview_GetEditControl(hTree) 'handle of TreeView edit control
Control Get Text hTree, GetDlgCtrlID(hTVEdit) To temp$ 'get new text
TreeView Get Select hDlg, %IDC_TreeView To hNode
TreeView Set Text hDlg, %IDC_TreeView, hNode, temp$
Function = 1 '1=new text 0=original text
' Control Set Focus hDlg, %IDC_TreeView
End Select
End Select
End Select
End Function
Sub BuildAcceleratorTable
Local c As Long, ac() As ACCELAPI, hAccelerator As Dword ' for keyboard accelator table values
Dim ac(0)
ac(0).fvirt = %FVIRTKEY
ac(0).key = %VK_F2
ac(0).cmd = %IDM_EditTree
Accel Attach hDlg, AC() To hAccelerator
End Sub
Sub AddMenu()
Menu New Bar To hMenu
Menu New Popup To hMenuEdit
Menu Add Popup, hMenu, "&Edit", hMenuEdit, %MF_Enabled
Menu Add String, hMenuEdit, "&Edit Tree Node" + $Tab + "F2", %IDM_EditTree, %MF_Enabled
Menu Add String, hMenuEdit, "&End Edit of Tree Node", %IDM_EndEditTree, %MF_Enabled
Menu Attach hMenu, hDlg
End Sub
'gbs_00266
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm