VirtualTreeView: properly handling selection changes

后端 未结 4 758
自闭症患者
自闭症患者 2021-02-02 01:26

This question will seem obvious to those who haven\'t encountered the problem themselves.

I need to handle selection changes in VTV. I have a flat list of nodes. I need

相关标签:
4条回答
  • 2021-02-02 01:57

    You forgot OnStateChange event. This event will be fired right after any selection change and you can then handle all selected nodes.

    procedure TForm1.vstStateChange(Sender: TBaseVirtualTree; Enter,
      Leave: TVirtualTreeStates);
    begin
      if tsChangePending in Leave then
        DoSomething;
    end;
    
    0 讨论(0)
  • 2021-02-02 02:03

    I assume that you might have used the answers given here or even found another solution but I would like to contribute a bit here...

    In a NON-Multiselect environment (I have not tested it in a multi-select environment) I have found a quite simple solution without the delay:

    Keep a global PVirtualNode pointer (Lets call it FSelectedTreeNode). On startup obviously you will assign nil to it.

    Now eveytime you use your arrow keyboard keys to select the next node the OnTreeChange will happen twice. Once for the node that will be deselected and once for the newly selected node. In your OnTreeChange event you do the following:

      If Node <> FSelectedTreeNode then
        begin
          FSelectedTreeNode := Node;
          If Node = nil then
            {Do some "Node Deselected" code}
          else
            {Do whatever you want to do when a new node is selected}
        end;
    

    This works quite well with my code and it has no flicker and at least no delay.

    The trick is that the newly selected node will be assigned to the global pointer and it will happen last. So when you select another node afterwards it will not do anything on the first OnTreeChange because then the global pointer will be the same as the node being deselected.

    0 讨论(0)
  • 2021-02-02 02:10

    Set the ChangeDelay property to an appropriate, greater than zero value in milliseconds, e.g. 100. This implements the one-shot timer Rob Kennedy suggests in his answer.

    0 讨论(0)
  • 2021-02-02 02:19

    Use a one-shot timer. When the timer fires, check whether the selection is different, update your display if it is, and disable the timer. Each time you receive a potential selection-changing event (which I think is always OnChange), reset the timer.

    This gives you a way of waiting for the event you really want and avoid flickering. The cost is a slightly delayed UI.

    0 讨论(0)
提交回复
热议问题