Is it possible to select multiple columns in Virtual Treeview?
Asked Answered
L

3

6

I need to add functionality to copy a rectangular selection of nodes and columns, but I can't find any way to actually select multiple columns in a Virtual Treeview (beside toFullRowSelect).

Am I just missing something? And if not, is there a descendant out there with grid-like multicolumn select support?

Leslielesly answered 29/1, 2009 at 10:52 Comment(0)
L
6

So after some testing I came up with the following, thanks DiGi for the extra push. DrawSelection won't work with this solution so it needs to be disabled. Since I don't think I'll need to do this again soon I didn't write a descendant.

Set toDisableDrawSelection, toExtendedFocus and toMultiSelect to True.

Declare the following variables/properties somewhere suitable:

StartSelectedColumn: integer;
FirstSelectedColumn: integer;
LastSelectedColumn: integer;
Selecting: boolean;

Update the following events:

OnKeyDown

if (not Selecting) and (Key = VK_SHIFT) then
begin
  StartSelectedColumn := vtMain.FocusedColumn;
  FirstSelectedColumn := StartSelectedColumn;
  LastSelectedColumn := StartSelectedColumn;
  Selecting := true;
end;

OnKeyUp

if Key = VK_SHIFT then
  Selecting := false;

OnFocusChanged

if Selecting then
begin
  if column < StartSelectedColumn then
  begin
    FirstSelectedColumn := column;
    LastSelectedColumn := StartSelectedColumn;
  end
  else if column > StartSelectedColumn then
  begin
    FirstSelectedColumn := StartSelectedColumn;
    LastSelectedColumn := column
  end
  else
  begin
    FirstSelectedColumn := column;
    LastSelectedColumn := column;
  end;
end
else
begin
  StartSelectedColumn := column;
  FirstSelectedColumn := column;
  LastSelectedColumn := column;
end;

OnBeforeCellPaint

if vtMain.Selected[node] and InRange(column, FirstSelectedColumn, LastSelectedColumn) then
begin
  if vtMain.Focused then
    TargetCanvas.Brush.Color := vtMain.Colors.FocusedSelectionColor
  else
    TargetCanvas.Brush.Color := vtMain.Colors.UnfocusedSelectionColor;
  TargetCanvas.Brush.Style := bsSolid;
  TargetCanvas.FillRect(CellRect);
end;

OnPaintText

if vtMain.Selected[node] and InRange(column, FirstSelectedColumn, LastSelectedColumn) then
begin
  if vtMain.Focused then
    TargetCanvas.Font.Color := clHighlightText
  else
    TargetCanvas.Font.Color := vtMain.Font.Color;
end;
Leslielesly answered 29/1, 2009 at 14:30 Comment(0)
F
1

You can try enable/add toGridExtensions in TreeOptions.MiscOptions. It enables free moving in columns by cursor keys, but VT still deselect column on leaving. But I'm sure that is possible to "fix" it by custom draw and remembering starting node and column.

Frei answered 29/1, 2009 at 12:29 Comment(1)
I've tried this and it doesn't work very well when you want to use a mouse. It does however "solve" the problem so upvote. :)Leslielesly
F
1

One more tip - look at OnStateChange event, maybe you can use

procedure TSomeForm.VTreeStateChange(Sender: TBaseVirtualTree; Enter,Leave: TVirtualTreeStates);
begin
  if tsDrawSelecting in Enter then
  begin
    // Save position
  end;
end;
Frei answered 30/1, 2009 at 11:17 Comment(1)
Interesting, I'll look at it later. :) Thanks!Leslielesly

© 2022 - 2024 — McMap. All rights reserved.