Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2013-02-01
12
Medium Priority
?
2,616 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 28

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 28

Accepted Solution

by:
Sinisa Vuk earned 2000 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 28

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 28

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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

916 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