Recent

Author Topic: TVirtualStringTree validating data and OnEdited event  (Read 183 times)

inferno

  • New Member
  • *
  • Posts: 10
TVirtualStringTree validating data and OnEdited event
« on: December 02, 2019, 03:00:17 pm »
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:

Code: Pascal  [Select]
  1. procedure TMainForm.VSTProjectsEdited(Sender: TBaseVirtualTree;
  2.   Node: PVirtualNode; Column: TColumnIndex);
  3. var
  4.   EditRec: PProjectRec;
  5.   CheckRec: PProjectRec;
  6.   CheckNode: PVirtualNode;
  7. begin
  8.   if Assigned(Node) and (Column=0) then
  9.   begin
  10.     EditRec := Sender.GetNodeData(Node);
  11.     CheckNode := Sender.GetFirst;
  12.     while Assigned(CheckNode) do
  13.     begin
  14.       if (CheckNode<>Node) then
  15.       begin
  16.         CheckRec:= Sender.GetNodeData(CheckNode);
  17.         if (CheckRec^.Name=EditRec^.Name) then
  18.         begin
  19.           ShowMessage('Project name must be unique');
  20.           Sender.EditNode(Node, Column);
  21.           break;
  22.         end;
  23.       end;
  24.       CheckNode := Sender.GetNextSibling(CheckNode);
  25.     end;
  26.   end;
  27. 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:

Code: Pascal  [Select]
  1. procedure TMainForm.VSTProjectsFocusChanging(Sender: TBaseVirtualTree; OldNode,
  2.   NewNode: PVirtualNode; OldColumn, NewColumn: TColumnIndex;
  3.   var Allowed: Boolean);
  4. begin
  5.   if Sender.IsEditing then
  6.   begin
  7.     Allowed:=false;
  8.   end;
  9. 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
« Last Edit: December 02, 2019, 03:01:55 pm by inferno »

GetMem

  • Hero Member
  • *****
  • Posts: 3519
Re: TVirtualStringTree validating data and OnEdited event
« Reply #1 on: December 03, 2019, 08:28:20 am »
Please try attached project(Tested with: Lazarus Trunk/FPC 3.0.4/Windows 64 bit).

inferno

  • New Member
  • *
  • Posts: 10
Re: TVirtualStringTree validating data and OnEdited event
« Reply #2 on: December 07, 2019, 06:55:57 pm »
Thanks GetMem for the code! Now the validation works like it should:)