BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Filling Tree View

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi VCL Components Writing
View previous topic :: View next topic  
Author Message
Ricardo Oliveira
Guest





PostPosted: Tue Nov 11, 2003 10:46 am    Post subject: Filling Tree View Reply with quote



Hi!

Is there anyway to fill a treeview realy fast?
I'm filing a treeview with items that are copied from a table (nr. fields *
nr. records). The problem is it gets very slow.
Anyway of doing this faster?

Hope I made my self clear.
Ricardo Oliveira


Back to top
John Leavey
Guest





PostPosted: Tue Nov 11, 2003 10:58 am    Post subject: Re: Filling Tree View Reply with quote



On Tue, 11 Nov 2003 10:46:37 -0000, "Ricardo Oliveira"
<ricardooliveira(removethis)@jpmabreu.pt> wrote:

Quote:
Hi!

Is there anyway to fill a treeview realy fast?
I'm filing a treeview with items that are copied from a table (nr. fields *
nr. records). The problem is it gets very slow.
Anyway of doing this faster?

1) Make sure that the code that populates the tree view is enclosed in

TV.Items.BeginUpdate;
try
...
finally
TV.Items.EndUpdate;
end;


2) If this is still not fast enough for you, try using Mike Lischke's Virtual Tree View -
www.delphi-gems.com, although this requires a slightly different mindset in relation to
data handling than a standard tree view.



John Leavey

Back to top
Ricardo Oliveira
Guest





PostPosted: Tue Nov 11, 2003 12:02 pm    Post subject: Re: Filling Tree View Reply with quote



Hi John!
Thanks for your sugestion.

I've tested BeginUpdate and it works but it is not fast enough.
I have also seen Mike Lischke's Virtual Tree View but I was expecting
something less complicated.
Any chance of using load(Stream, File, etc.) to load the items?
Other Sugestions? :)

Thanks again.
Ricardo Oliveira.

"John Leavey" <johnl (AT) NO_SPAM (DOT) compufile.co.uk> escreveu na mensagem
news:41g1rvg645lqgrcmgjues3jphvpve7qtu5 (AT) 4ax (DOT) com...
Quote:
On Tue, 11 Nov 2003 10:46:37 -0000, "Ricardo Oliveira"
ricardooliveira(removethis)@jpmabreu.pt> wrote:

Hi!

Is there anyway to fill a treeview realy fast?
I'm filing a treeview with items that are copied from a table (nr. fields
*
nr. records). The problem is it gets very slow.
Anyway of doing this faster?

1) Make sure that the code that populates the tree view is enclosed in

TV.Items.BeginUpdate;
try
...
finally
TV.Items.EndUpdate;
end;


2) If this is still not fast enough for you, try using Mike Lischke's
Virtual Tree View -
www.delphi-gems.com, although this requires a slightly different mindset
in relation to
data handling than a standard tree view.



John Leavey



Back to top
Constantine Yannakopoulos
Guest





PostPosted: Tue Nov 11, 2003 1:24 pm    Post subject: Re: Filling Tree View Reply with quote

Author := "Ricardo Oliveira";

Quote:
I've tested BeginUpdate and it works but it is not fast enough.
I have also seen Mike Lischke's Virtual Tree View but I was expecting
something less complicated.
Any chance of using load(Stream, File, etc.) to load the items?
Other Sugestions? Smile

Yes, load the nodes incrementally. <g>

I assume that you have only two levels of nodes: records and then field
values per record. Start by loading the records in the treeview and put
a [+] sign to them without adding any child nodes by setting
HasChildren to True. Then write an OnExpanding event handler that loads
the child nodes (fields) of a record if they are not already loaded
(the test for "not already loaded" is: if Node.HasChildren and not
Assigned(Node.getFirstChild) then ...)

This way at the time you populate the treeview you do a single loop
instead of a double one. Also you do not load fields for a record in
the treeview unless the user explicitly requests you to by clicking the
[+] sign.

HTH

--
Constantine

Back to top
Ricardo Oliveira
Guest





PostPosted: Tue Nov 11, 2003 4:01 pm    Post subject: Re: Filling Tree View Reply with quote

Hi Constantine!
Thanks for your help.
I've tried your sugestion but i'm having trouble handling the OnExpanding
event.

Thanks for your help
Ricardo Oliveira.

"Constantine Yannakopoulos" <kyan (AT) deltasingular (DOT) remove.this.gr> escreveu na
mensagem news:3fb0f134$1 (AT) newsgroups (DOT) borland.com...
Quote:
Author := "Ricardo Oliveira";

| I've tested BeginUpdate and it works but it is not fast enough.
| I have also seen Mike Lischke's Virtual Tree View but I was expecting
| something less complicated.
| Any chance of using load(Stream, File, etc.) to load the items?
| Other Sugestions? :)

Yes, load the nodes incrementally.
I assume that you have only two levels of nodes: records and then field
values per record. Start by loading the records in the treeview and put
a [+] sign to them without adding any child nodes by setting
HasChildren to True. Then write an OnExpanding event handler that loads
the child nodes (fields) of a record if they are not already loaded
(the test for "not already loaded" is: if Node.HasChildren and not
Assigned(Node.getFirstChild) then ...)

This way at the time you populate the treeview you do a single loop
instead of a double one. Also you do not load fields for a record in
the treeview unless the user explicitly requests you to by clicking the
[+] sign.

HTH

--
Constantine



Back to top
Ricardo Oliveira
Guest





PostPosted: Tue Nov 11, 2003 5:33 pm    Post subject: Re: Filling Tree View Reply with quote

Could someone help me please?!

Ricardo Oliveira

"Ricardo Oliveira" <ricardooliveira(removethis)@jpmabreu.pt> escreveu na
mensagem news:3fb10710$1 (AT) newsgroups (DOT) borland.com...
Quote:
Hi Constantine!
Thanks for your help.
I've tried your sugestion but i'm having trouble handling the OnExpanding
event.

Thanks for your help
Ricardo Oliveira.

"Constantine Yannakopoulos" <kyan (AT) deltasingular (DOT) remove.this.gr> escreveu
na
mensagem news:3fb0f134$1 (AT) newsgroups (DOT) borland.com...
Author := "Ricardo Oliveira";

| I've tested BeginUpdate and it works but it is not fast enough.
| I have also seen Mike Lischke's Virtual Tree View but I was expecting
| something less complicated.
| Any chance of using load(Stream, File, etc.) to load the items?
| Other Sugestions? :)

Yes, load the nodes incrementally.
I assume that you have only two levels of nodes: records and then field
values per record. Start by loading the records in the treeview and put
a [+] sign to them without adding any child nodes by setting
HasChildren to True. Then write an OnExpanding event handler that loads
the child nodes (fields) of a record if they are not already loaded
(the test for "not already loaded" is: if Node.HasChildren and not
Assigned(Node.getFirstChild) then ...)

This way at the time you populate the treeview you do a single loop
instead of a double one. Also you do not load fields for a record in
the treeview unless the user explicitly requests you to by clicking the
[+] sign.

HTH

--
Constantine





Back to top
Harley Pebley
Guest





PostPosted: Tue Nov 11, 2003 9:22 pm    Post subject: Re: Filling Tree View Reply with quote

Quote:
I've tried your sugestion but i'm having trouble handling the OnExpanding
event.

Could someone help me please?!

Perhaps, if you explained what trouble you're having.

Back to top
Ricardo Oliveira
Guest





PostPosted: Wed Nov 12, 2003 12:53 am    Post subject: Re: Filling Tree View Reply with quote

Sorry my fault! Smile
I don't know how to handle the OnExpanding event.
Maybe someone who did this before can help me.

"Harley Pebley" <harley_pebley (AT) idahotech (DOT) com> escreveu na mensagem
news:9bbni84ylege$.pog3p5k5uy8y.dlg (AT) 40tude (DOT) net...
Quote:
I've tried your sugestion but i'm having trouble handling the
OnExpanding
event.

Could someone help me please?!

Perhaps, if you explained what trouble you're having.



Back to top
Harley Pebley
Guest





PostPosted: Wed Nov 12, 2003 1:22 am    Post subject: Re: Filling Tree View Reply with quote

Quote:
I've tried your sugestion but i'm having trouble handling the
OnExpanding
event.

Could someone help me please?!

Perhaps, if you explained what trouble you're having.

I don't know how to handle the OnExpanding event.

Ah. You're given a Node parameter. Add the children to that node that
should be there.

For example, create a new, blank application. Drop a TreeView on it and
attach the following code. When run, it will only have three things in the
list at the start but as the user clicks, it will keep adding and adding
and adding and... but only on the item which is clicked. The others will be
empty until they are clicked on.

procedure TForm1.FormCreate(Sender: TObject);
begin
TreeView1.Items.Add(nil, 'Item 1').HasChildren := True;
TreeView1.Items.Add(nil, 'Item 2').HasChildren := True;
TreeView1.Items.Add(nil, 'Item 3').HasChildren := True;
end;

procedure TForm1.TreeView1Expanding(Sender: TObject; Node: TTreeNode;
var AllowExpansion: Boolean);
begin
TreeView1.Items.AddChild(Node, Node.Text+'.1').HasChildren := True;
TreeView1.Items.AddChild(Node, Node.Text+'.2').HasChildren := True;
TreeView1.Items.AddChild(Node, Node.Text+'.3').HasChildren := True;
end;

HTH,
Harley Pebley

Back to top
Ricardo Oliveira
Guest





PostPosted: Wed Nov 12, 2003 1:33 am    Post subject: Re: Filling Tree View Reply with quote

I'm so sorry but i can't quite explain me.

I'm creating an object inherited from a treeview and I need to handle the
OnExpanding event so that when the node is expanded I can fill it with it's
children (read posts above).
Sorry, I guess I need some sleep. :)

"Harley Pebley" <harley_pebley (AT) idahotech (DOT) com> escreveu na mensagem
news:tchstzeijrf0.1iq85vor51zq4$.dlg (AT) 40tude (DOT) net...
Quote:
I've tried your sugestion but i'm having trouble handling the
OnExpanding
event.

Could someone help me please?!

Perhaps, if you explained what trouble you're having.

I don't know how to handle the OnExpanding event.

Ah. You're given a Node parameter. Add the children to that node that
should be there.

For example, create a new, blank application. Drop a TreeView on it and
attach the following code. When run, it will only have three things in the
list at the start but as the user clicks, it will keep adding and adding
and adding and... but only on the item which is clicked. The others will
be
empty until they are clicked on.

procedure TForm1.FormCreate(Sender: TObject);
begin
TreeView1.Items.Add(nil, 'Item 1').HasChildren := True;
TreeView1.Items.Add(nil, 'Item 2').HasChildren := True;
TreeView1.Items.Add(nil, 'Item 3').HasChildren := True;
end;

procedure TForm1.TreeView1Expanding(Sender: TObject; Node: TTreeNode;
var AllowExpansion: Boolean);
begin
TreeView1.Items.AddChild(Node, Node.Text+'.1').HasChildren := True;
TreeView1.Items.AddChild(Node, Node.Text+'.2').HasChildren := True;
TreeView1.Items.AddChild(Node, Node.Text+'.3').HasChildren := True;
end;

HTH,
Harley Pebley



Back to top
Marc Rohloff
Guest





PostPosted: Wed Nov 12, 2003 2:03 pm    Post subject: Re: Filling Tree View Reply with quote

Quote:
I'm creating an object inherited from a treeview and I need to handle the
OnExpanding event so that when the node is expanded I can fill it with it's
children (read posts above).
The first thing you need to be able to do is to figure out which node you

are actually at given a TreeNode instance. This is necessary in all sorts
of events so it is useful to get it right and maybe seperate it into a
function.

You could use the index, absoluteindex or parent values to figure this
out. Another way is to store some information in the Data property of
each node as you create it. A third alternative if you are subclassing
the tree view is to supply your own custom implementation of Ttreenode.

Once you can do this then in the OnExpanding event you can figure out the
node you are in and then fill in all it's children.

Marc

Back to top
Harley Pebley
Guest





PostPosted: Wed Nov 12, 2003 7:13 pm    Post subject: Re: Filling Tree View Reply with quote

Quote:
I'm creating an object inherited from a treeview and I need to handle the
OnExpanding event so that when the node is expanded I can fill it with it's
children (read posts above).
Sorry, I guess I need some sleep. Smile

Ah. I think perhaps the piece you're missing is overriding the CanExpand
method? For example, create a blank application and replace the code in
Unit1.pas with:

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,
Forms,
Dialogs, StdCtrls, ComCtrls;

type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
TreeView1: TTreeView;
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

type
TMyTreeView = class(TTreeView)
protected
function CanExpand(Node: TTreeNode): Boolean; override;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
TreeView1 := TMyTreeView.Create(Self);
TreeView1.Parent := Self;
TreeView1.Align := alClient;
TreeView1.Items.Add(nil, 'Item 1').HasChildren := True;
TreeView1.Items.Add(nil, 'Item 2').HasChildren := True;
TreeView1.Items.Add(nil, 'Item 3').HasChildren := True;
end;

{ TMyTreeView }

function TMyTreeView.CanExpand(Node: TTreeNode): Boolean;
begin
result := inherited CanExpand(Node);
if result then
begin
TMyTreeView(Node.TreeView).Items.AddChild(Node,
Node.Text+'.1').HasChildren := True;
TMyTreeView(Node.TreeView).Items.AddChild(Node,
Node.Text+'.2').HasChildren := True;
TMyTreeView(Node.TreeView).Items.AddChild(Node,
Node.Text+'.3').HasChildren := True;
end;
end;

end.

Calling the inherited CanExpand will call the OnExpanding event handler. If
that returns true, then your custom code can run. This allows users of your
component to not expand the node if they so desire. If you don't ever want
them to have this feature, then you should descend from TCustomTreeView and
not expose the OnExpanding property.

HTH,
Harley Pebley

Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi VCL Components Writing All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.