Solved

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

Posted on 2013-02-01
12
2,261 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 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Using FastMM4... should I enable MMX option? 9 76
Copy file in dll not working but working on exe ! 18 88
Path  to current project in Delphi. 2 72
CheckListBox usage 3 51
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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

895 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

14 Experts available now in Live!

Get 1:1 Help Now