我有一个类型的组件TVirtualStringTree
(让我们称之为VST
).它具有列表形式的节点,即所有节点处于同一级别.我想在删除节点后使用该DeleteNode
方法更改焦点并使用OnFreeNode
事件:
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;
Run Code Online (Sandbox Code Playgroud)
我还希望更改引起一些反应,例如设置Enabled
按钮的属性:
procedure TMyForm.VSTFocusChanged(Sender: TBaseVirtualTree; Node: PVirtualNode; Column: TColumnIndex);
begin
btn1.Enabled := Assigned(Node);
end;
Run Code Online (Sandbox Code Playgroud)
但是解决方案存在一些问题.例如,当我使用"取消"按钮关闭表单(表单是使用ShowModal
方法打开)时,节点被释放,VSTFocusChanged
被触发,并且由于nil按钮,后者会抛出异常.当然,我可以检查按钮是否已分配,但在删除节点后是否有更优雅的解决方案更改焦点而没有这种不良影响(并且没有其他不良影响)?