I have a component of type TVirtualStringTree
(let us call it VST
). It has nodes in the form of a list, i.e. all the nodes are at the same level. I want to change a focus after deleting a node (with the DeleteNode
method) and I used OnFreeNode
event:
procedure TMyForm.VSTFreeNode(Sender: TBaseVirtualTree; Node: PVirtualNode);
var
NewFocus: PVirtualNode;
begin
NewFocus := VST.GetNext(Node);
if not Assigned(newFocus) then
NewFocus := VST.GetPrevious(Node);
if Assigned(NewFocus) then
begin
VST.FocusedNode := NewFocus;
VST.Selected[NewFocus] := True
end;
end;
I want also the change to cause some reactions, e.g. set the Enabled
property of a button:
procedure TMyForm.VSTFocusChanged(Sender: TBaseVirtualTree; Node: PVirtualNode; Column: TColumnIndex);
begin
btn1.Enabled := Assigned(Node);
end;
But there are some problems with the solution. For example, when I close the form with the Cancel button (the form is open with ShowModal
method), the nodes are freed, VSTFocusChanged
is triggered, and the later throws an exception because of nil button. Of course, I could check if the button is assigned, but is there more elegant solution of changing the focus after deleting a node without this undesirable effect (and without other undesirable effects)?