Solved

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

Posted on 2013-02-01
12
2,214 Views
Last Modified: 2013-02-07
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
Comment
Question by:Delphi_developer
  • 7
  • 4
12 Comments
 
LVL 19

Expert Comment

by:Thommy
ID: 38848360
A treeview has its own vertical scrollbar.
I suggest placing the treeview outside the scrollbox?
0
 

Author Comment

by:Delphi_developer
ID: 38848803
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
 

Author Comment

by:Delphi_developer
ID: 38848835
Increasing points to 500.
0
 
LVL 25

Expert Comment

by:Sinisa Vuk
ID: 38849239
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
 

Author Comment

by:Delphi_developer
ID: 38849291
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
 
LVL 25

Accepted Solution

by:
Sinisa Vuk earned 500 total points
ID: 38850187
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:Delphi_developer
ID: 38851959
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
 
LVL 25

Expert Comment

by:Sinisa Vuk
ID: 38852088
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
 

Author Comment

by:Delphi_developer
ID: 38852119
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
 
LVL 25

Expert Comment

by:Sinisa Vuk
ID: 38857105
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
 

Author Comment

by:Delphi_developer
ID: 38857275
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
 

Author Closing Comment

by:Delphi_developer
ID: 38865018
Thank you, works perfect!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now