Hi All,
I try to use OnEdit event for ensuring that field is unique for all nodes in the same level. My code is as follows:
procedure TMainForm.VSTProjectsEdited(Sender: TBaseVirtualTree;
Node: PVirtualNode; Column: TColumnIndex);
var
EditRec: PProjectRec;
CheckRec: PProjectRec;
CheckNode: PVirtualNode;
begin
if Assigned(Node) and (Column=0) then
begin
EditRec := Sender.GetNodeData(Node);
CheckNode := Sender.GetFirst;
while Assigned(CheckNode) do
begin
if (CheckNode<>Node) then
begin
CheckRec:= Sender.GetNodeData(CheckNode);
if (CheckRec^.Name=EditRec^.Name) then
begin
ShowMessage('Project name must be unique');
Sender.EditNode(Node, Column);
break;
end;
end;
CheckNode := Sender.GetNextSibling(CheckNode);
end;
end;
end;
But this doesn't work as it should. When editing is finished by clicking outside of the VSTProjects tree everythig works as expected - the message is shown once and user is forced to editing the node again. When editing is finished by pressing Enter key the message is show twice. And finally when editing is finished by clicking in the other node in the VSTProjects tree, the message occurs three times. So for some reason the OnEdited event is triggered few times when I try to activate editing again in the code. Without
Sender.EditNode(Node, Column); the OnEdited event is fired once and hence the message is shown once as expected, regardless of exit method from the editing process.
I made some improvement:
procedure TMainForm.VSTProjectsFocusChanging(Sender: TBaseVirtualTree; OldNode,
NewNode: PVirtualNode; OldColumn, NewColumn: TColumnIndex;
var Allowed: Boolean);
begin
if Sender.IsEditing then
begin
Allowed:=false;
end;
end;
This disallow users exit the editing process with two equal Name values and also decreases the number of viewed messages to two when clicking in other node.
How to limit the displayed message to one? Maybe there is some beter method to make such validations in TVirtualStringTree?
Also I found this
https://github.com/virtual-treeview/virtual-treeview/issues/118, but this is old case and it looks this was finally not a bug.
Best regards,
Inferno