Search code examples
delphiheaderdelphi-7double-clickvirtualtreeview

How to stop the sort symbol from hiding when TVirtualTreeView (TVirtualStringTree) header is double clicked


I am using a VirtualStringTree control as a list view and using the sort features. However when I double click the VirtualStringTree header the sort direction symbol hides until I click the header again.

Can that behaviour be disabled?

Things that I have tried but do not work:

  • I have searched the properties and cannot find a related setting
  • I have linked the double click header event to the click header event

My environment is Delphi 2007 Pro, Windows 7 Pro 64bit.


Solution

  • I had the same issue with double-click and hiding of sorting triangle and instead I just wanted a simple toggle up/down with nothing else. This issue is present unfortunately in latest VirtualTreeView (4.8.7) as well.

    Here is a bit of code that fixes the issue - put something like this in your OnHeaderClick event (not OnHeaderDblClick !).

    The relevant line is if HitInfo.Column = NoColumn then Exit; which fixes the double-click problem. You may or may not use the rest of code for your own purposes but it may be useful to someone else. The rest of explanation is in the code comments.

    You don't need to define OnHeaderDblClick event - it may be empty if not needed so you may want to remove that from your code.

    UPDATE

    Also read comments from TLama as it seems that with version 5.0.0. this fix may not operate as intended. With the current version it does though.

      {**
        A column header of a VirtualStringTree was clicked: Toggle the sort direction
      }
      procedure TMainForm.vstHeaderClick(Sender: TVTHeader; HitInfo: TVTHeaderHitInfo);
      begin
    
      // Don't call sorting procedure on right click
      // Some list-headers have a contextmenu which should popup then.
      if HitInfo.Button = mbRight then Exit;
    
      // Beginning with VT's r181, this proc is also called when doubleclicking-to-autofit
      // Seems buggy in VT as this suddenly calls it with Column=-1 in those cases.
      // See also issue #1150
      if HitInfo.Column = NoColumn then Exit;
    
      if Sender.SortColumn <> HitInfo.Column then Sender.SortColumn := HitInfo.Column
      else if Sender.SortDirection = sdAscending then Sender.SortDirection := sdDescending
      else Sender.SortDirection := sdAscending;
    
      Sender.Treeview.SortTree( HitInfo.Column, Sender.SortDirection );
      end;