Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2692
  • Last Modified:

How to scroll to selected node in Virtual Treeview in ScrollBox?

Hi

I have a Scrollbox with multiple controls, some labels and virtual treeviews. I know I can use ScrollInView to scroll into view a chosen control. And I know I can use ScrollIntoView to scroll treeview to the selected node.
But, if treeview is large (that is the reason it is in scrollbox), then I can't make it work to scroll a Scrollbox to see the selected node.

So, I can scroll to the top of treeview, but then I need a treeview to scroll to the selected node. It doesn't work, because treeivew is fully expanded, so for the treeview, the node is always visible.

To clarify more: assume Scrollbox visible area is around 50 nodes in treeview, and if selected node is node 60, I don't how to scroll Scrollbox to the areas where this node would be visible.

Any suggestions?

Thanx!
0
Delphi_developer
Asked:
Delphi_developer
  • 7
  • 4
1 Solution
 
ThommyCommented:
A treeview has its own vertical scrollbar.
I suggest placing the treeview outside the scrollbox?
0
 
Delphi_developerAuthor Commented:
Hi,

thank you, but I can't. I have 10 VTVs (virtual treeviews) in single scroll box, since each one has it's own data and data structure (columns, col widths).

All VTV are complete (height is full) so all nodes are visible even if it has 100s of them, that is the reason they are in Scrollbox - to have one loooong view, and Scrollbox's scrollbar to go up and down.

I was thinking of using ScroollInView to get to chosen VTV and then do another scroll by node-line-number * line-height, for example setting VertScrollBar.Position... but this doesn't sound like neat solution....

Actuallly while I write this I thought maybe I can change procedure TScrollingWinControl.ScrollInView(AControl: TControl); to scroll to Nodes's rectangle... but have no idea how to get node's rectangle... would that work at all?

Thanx
0
 
Delphi_developerAuthor Commented:
Increasing points to 500.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Sinisa VukCommented:
This is my simple example. Your problem is treeview is bigger than box, so this needs another approach.

procedure ScrollRectInView(Rect: TRect; scBox: TScrollBox);
begin
  if Rect.Left < 0 then
    scBox.HorzScrollBar.Position := scBox.HorzScrollBar.Position + Rect.Left
  else if Rect.Right > scBox.ClientWidth then
  begin
    Rect.Right := Math.Min(Rect.Right, Rect.Left + scBox.ClientWidth);
  end;
  if Rect.Top < 0 then
    scBox.VertScrollBar.Position := scBox.VertScrollBar.Position + Rect.Top
  else if Rect.Bottom > scBox.ClientHeight then
  begin
    Rect.Bottom := Math.Min(Rect.Bottom, Rect.Top + scBox.ClientHeight);
  end;

  scBox.HorzScrollBar.Position :=
    scBox.HorzScrollBar.Position + Rect.Right - scBox.ClientWidth;

  scBox.VertScrollBar.Position :=
    scBox.VertScrollBar.Position + Rect.Bottom - scBox.ClientHeight;
end;

procedure ScrollNodeToView(node: PVirtualNode; VST: TVirtualStringTree; scBox: TScrollBox);
var
  Rect: TRect;
begin
  //scroll node first
  VST.TreeOptions.PaintOptions := VST.TreeOptions.PaintOptions - [toHideSelection];
  VST.ClearSelection;

  node := VST.GetLast();

  VST.ScrollIntoView(node, False, False);
  VST.Selected[node] := True;

  //get display rect of selected node
  Rect := VST.GetDisplayRect(node, NoColumn, not (toGridExtensions in VST.TreeOptions.MiscOptions));

  //go to screen coordiantes
  Rect.TopLeft := VST.ClientToScreen(Rect.TopLeft);
  Rect.BottomRight := VST.ClientToScreen(Rect.BottomRight);

  //go to scrollbox
  Rect.TopLeft := scBox.ScreenToClient(Rect.TopLeft);
  Rect.BottomRight := scBox.ScreenToClient(Rect.BottomRight);

  //possible margins
  Dec(Rect.Left, scBox.HorzScrollBar.Margin);
  Inc(Rect.Right, scBox.HorzScrollBar.Margin);
  Dec(Rect.Top, scBox.VertScrollBar.Margin);
  Inc(Rect.Bottom, scBox.VertScrollBar.Margin);

  //scroll box at last
  ScrollRectInView(Rect, scBox);
end;

procedure TMainForm.Button2Click(Sender: TObject);
var
  node: PVirtualNode;
begin
  node := VST.GetLast();
  ScrollNodeToView(node, VST, ScrollBox1);
end;

Open in new window

0
 
Delphi_developerAuthor Commented:
Thank you sinisav, this looks promising. I changed GetLast(), because I know the exact node which I want to show. I just commented both GetLast() lines and it works, but the node is always at the bottom of scrollbox. Better, but I would prefer to have it shown at the center of scrollbox.

Or was the GetLast() there for a reason? But it was always selectin and centering last row... not what I need.

I didn't explain what I'm doing this for: I'm making a comparison tool and all VTVs are all comparison data and I'm implementing Next/Prev change... this must walk through all the VTVs and 'changed' (in data) nodes and show them (center them) so they are clearly visible, one by one. That's why I need all in one long scroll box.

Thanx for further information!
0
 
Sinisa VukCommented:
GetLast() is just to get some node. You just pass some other. Upper example scroll just in view. To scroll in center there is one little modification:

procedure ScrollRectInView(Rect: TRect; scBox: TScrollBox);
begin
  if Rect.Left < 0 then
    scBox.HorzScrollBar.Position := scBox.HorzScrollBar.Position + Rect.Left
  else if Rect.Right > scBox.ClientWidth then
  begin
    Rect.Right := Math.Min(Rect.Right, Rect.Left + scBox.ClientWidth);
  end;
  if Rect.Top < 0 then
    scBox.VertScrollBar.Position := scBox.VertScrollBar.Position + Rect.Top
  else if Rect.Bottom > scBox.ClientHeight then
  begin
    Rect.Bottom := Math.Min(Rect.Bottom, Rect.Top + scBox.ClientHeight);
  end;

  scBox.HorzScrollBar.Position :=
    scBox.HorzScrollBar.Position + Rect.Right - scBox.ClientWidth;

  scBox.VertScrollBar.Position :=
    scBox.VertScrollBar.Position + Rect.Bottom - (scBox.ClientHeight div 2);
end;

procedure ScrollNodeToView(node: PVirtualNode; VST: TVirtualStringTree; scBox: TScrollBox);
var
  Rect: TRect;
begin
  //scroll node first
  VST.TreeOptions.PaintOptions := VST.TreeOptions.PaintOptions - [toHideSelection];
  VST.ClearSelection;

  VST.ScrollIntoView(node, False, False);
  VST.Selected[node] := True;

  //get display rect of selected node
  Rect := VST.GetDisplayRect(node, NoColumn, not (toGridExtensions in VST.TreeOptions.MiscOptions));

  //go to screen coordiantes
  Rect.TopLeft := VST.ClientToScreen(Rect.TopLeft);
  Rect.BottomRight := VST.ClientToScreen(Rect.BottomRight);

  //go to scrollbox
  Rect.TopLeft := scBox.ScreenToClient(Rect.TopLeft);
  Rect.BottomRight := scBox.ScreenToClient(Rect.BottomRight);

  //possible margins
  Dec(Rect.Left, scBox.HorzScrollBar.Margin);
  Inc(Rect.Right, scBox.HorzScrollBar.Margin);
  Dec(Rect.Top, scBox.VertScrollBar.Margin);
  Inc(Rect.Bottom, scBox.VertScrollBar.Margin);

  //scroll box at last
  ScrollRectInView(Rect, scBox);
end;

procedure TMainForm.Button2Click(Sender: TObject);
var
  node: PVirtualNode;
begin
  node := VST.GetLast();
  ScrollNodeToView(node, VST, ScrollBox1);
end;

Open in new window

0
 
Delphi_developerAuthor Commented:
Thank you, this works!

It just flickers a lot... I'm looking into my code if there's anything I can do... is there a piece of your code that can be optimized?
0
 
Sinisa VukCommented:
Works very fast for me, but you can always try this:
...
ScrollBox1.DoubleBuffered := True;
  VST.BeginUpdate;
  try
    node := VST.GetLast();
    ScrollNodeToView(node, VST, ScrollBox1);
  finally
    VST.EndUpdate;
  end;
....

Open in new window

0
 
Delphi_developerAuthor Commented:
I have DobleBuffered enabled.

You can see it jumps up before centering... VTV is positioned on top and then centered on node. Is it possible that from the line :31 till the line :52 (in your example) it just takes a bit too long, and that is why the flicker is there?
0
 
Sinisa VukCommented:
Do you have some event attached to vtv? Maybe some drawing stuff? You can comment line by line and see which one produce flickering.
0
 
Delphi_developerAuthor Commented:
Hi, thank you, will do that. I have only OnBeforeCellPaint and OnPaintText, but these are quick and very small procedures.

It flickers when I move from one node to another in same VTV, up or down. I'll do some testing. Thanx!
0
 
Delphi_developerAuthor Commented:
Thank you, works perfect!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 7
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now