Programmatically edit a treeview label

The problem

The normal treeview class does not have a method of putting your treeview item into "edit mode" programmatically. Of course you can do this in the treeview when you are using it, but sometimes you have a need to set this state without requiring user interaction.

As if is a standard windows control, you can send it windows messages yourself. The message to send is TVM_EDITLABEL

Getting it to work

First you have to enable label editing with:

Set TreeEditLabelsState to True

Then you have to send WM_EDITLABEL for example as in:

Procedure EditNow
   Handle hItem
   Handle hWnd
   Integer iResult
   Get CurrentTreeItem to hItem
   Get Window_Handle to hWnd
   Move (SendMessage(hWnd,TVM_EDITLABEL,0,hItem)) to iResult
End_Procedure
On_Key Key_Alt+Key_F1 Send EditNow

As you can see I've not tested with doing it just after adding the item but in the moment that the control is waiting for user action. Normally the F2 is used so you might replace Alt+F1 with the F2 key, I did not do this to see if my code was really used instead of build in techniques.

Enable editmode on a new item

First, you need the Window_Handle of the control you want to send the message to. This means you can send this message ONLY when the controls is paged. Second, you need the handle of the treeviewitem to edit. Third, the property TreeEditLabelState is a treeview property and not for the item.

Correct syntax would be

 Procedure OnClick
   Handle hItem
   Get AddTreeItem of oTreeView1 "New item"  0 0 0 0 To hItem
   Set ItemData of oTreeView1 Item hItem to 999
   Get Window_Handle of oTreeView1 to hWnd
   If (hWnd <> 0) Begin
       Set TreeEditLabelsState of oTreeView1 to True
       Move (SendMessage(hWnd,TVM_EDITLABEL,0,hItem)) to iResult
       Set TreeEditLabelsState of oTreeView1 to False
   End
 End_Procedure

Depending on the version of VDF you are using you have the following events and properties

pbCancelEdit
OnBeginLabelEdit
OnEndLabelEdit

These are documented in the Help system

External references

Automatic Edit item label in Treeview

Put label in Editmode immediately

MSDN TVM_EDITLABEL Message