Solved

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

Posted on 2013-02-01
12
2,348 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 26

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 26

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
 

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 26

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 26

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

856 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