Autor Beitrag
jcop
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 27

WinXP, Vista
Delphi 2009, Vb.NET
BeitragVerfasst: Di 08.09.09 09:46 
Hello,

I'm trying following function.

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
function NodeExists(Nodes:TTreenodes; Text:String):Boolean;
var
  i     :Integer;
  sText :String;
begin
  Result := False;
  sText := AnsiUpperCase(Text);
  i := 0;
  while (i < Nodes.Count) and (Result = False) do begin
    Result := AnsiUpperCase(Nodes.Item[i].Text) = sText;
    Inc(i);
  end;
end;


But when I try to load my treeview the childnodes are only added for a part, whenever the acft is recreated the nodeexist function doesn't add new childnodes under the parentnode, but places them as level0 nodes.

-Boeing
------B737_1
------B737_2
-B747_1 This should also be placed under the Boeing Node.
-Cessna
-------C182_1
...

The actual loading code is this.
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
var
 uimannode : TtreeNode;
 i : integer;
 acft: TIniFile;
 MyPacft: Pacft;
 uiman: string;
 acTitle: string;
 x: integer;
 acftTitleNode: Ttreenode;


Begin

 Statusbar1.panels[0].text := Format('Searching in %s...',[AName]);
  statusbar1.Update;

    i :=0;

    acft := Tinifile.Create(Aname);

    while acft.SectionExists('fltsim.' + intToStr(i)) = true do
        begin
        new (MyPacft);
        MyPacft^.Title := acft.ReadString('fltsim.' + intToStr(i), 'Title', 'EMPTY');
        MyPacft^.Sim := acft.ReadString('fltsim.' + intToStr(i), 'SIM', 'EMPTY');
        MyPacft^.texture := acft.ReadString('fltsim.' +intToStr(i), 'Texture', 'EMPTY');
        MyPacft^.Model := acft.ReadString('fltsim.' + intToStr(i), 'MODEL', 'EMPTY');
        MyPacft^.atc_airline := acft.ReadString('fltsim.' + intToStr(i),'act_airline', 'EMPTY');
        MyPacft^.ui_manufacturer := acft.ReadString('fltsim.' + intToStr(i), 'ui_manufacturer', 'EMPTY');
        MyPacft^.FolderPath := AName;
        MyPacft^.FltSimID := i;

        if NOT NodeExists(TreeView2.Items, MyPacft^.ui_manufacturer) then
          uimannode := TreeView2.Items.AddObject(nil, MyPacft^.ui_manufacturer, mypacft);

        //if NodeExists(TreeView2.Items, MyPacft^.ui_manufacturer) then
        acftTitleNode := TreeView2.Items.AddChildObject(uimannode, MyPacft^.Title, MyPacft);
         //acftTitleNode := TreeView2.Items.AddChildObject(uimannode, MyPacft^.Title, MyPacft);
        i := i + 1;
        end;
end;


someone has an idea what i'm overlooking here?

regards and again thanks for all your efforts/inputs.
Johan

Moderiert von user profile iconNarses: Changed thread title, was: "Again TreeNode".
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10185
Erhaltene Danke: 1261

W11x64
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Di 08.09.09 12:00 
Moin!

I´d like to suggest this approach via a lookup-table due to the poor performance of indexed access to tree nodes: :nixweiss:
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
procedure TForm1.Button1Click(Sender: TObject);
  var
    uimannode: TtreeNode;
    i,x: Integer;
    acft: TIniFile;
    MyPacft: Pacft;
    ManLookup: TStringList;
begin
  Statusbar1.panels[0].text := Format('Searching in %s...',[AName]);
  Statusbar1.Update;
  i := 0;
  acft := TIniFile.Create(Aname);
  ManLookup := TStringList.Create;
  try
    ManLookup.CaseSensitive := FALSE; // no case
    ManLookup.Sorted := TRUE; // speed up things
    while acft.SectionExists('fltsim.' +IntToStr(i)) do begin // don´t compare booleans explicitly!
      New(MyPacft);
      MyPacft^.Title := acft.ReadString('fltsim.' +IntToStr(i), 'Title', 'EMPTY');
      MyPacft^.Sim := acft.ReadString('fltsim.' +IntToStr(i), 'SIM', 'EMPTY');
      MyPacft^.texture := acft.ReadString('fltsim.' +IntToStr(i), 'Texture', 'EMPTY');
      MyPacft^.Model := acft.ReadString('fltsim.' +IntToStr(i), 'MODEL', 'EMPTY');
      MyPacft^.atc_airline := acft.ReadString('fltsim.' +IntToStr(i),'act_airline', 'EMPTY');
      MyPacft^.ui_manufacturer := acft.ReadString('fltsim.' +IntToStr(i), 'ui_manufacturer', 'EMPTY');
      MyPacft^.FolderPath := AName;
      MyPacft^.FltSimID := i;
      x := ManLookup.IndexOf(MyPacft^.ui_manufacturer);
      if (x = -1) then begin // new manufacturer
        uimannode := TreeView2.Items.AddObject(NIL, MyPacft^.ui_manufacturer, mypacft); // create node
        ManLookup.AddObject(MyPacft^.ui_manufacturer, uimannode); // register new manufacturer
      end
      else // known manufacturer
        uimannode := TTreeNode(ManLookup.Objects[x]); // get node reference
      TreeView2.Items.AddChildObject(uimannode, MyPacft^.Title, MyPacft);
      Inc(i);
    end;
  finally
    ManLookup.Free;
    acft.Free;
  end;
end;
cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
jcop Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 27

WinXP, Vista
Delphi 2009, Vb.NET
BeitragVerfasst: Di 08.09.09 12:27 
Narses,

This is much better (especially concerning fast loading), however it doesn't seems to account if the parentnode already exist.

let me explain

ANAME is a sort of ini file, but I have let say 60 of them all in different folders.
What your code is doing if adding for each found ANAME file a parent,

-Boeing
----B737_1
----B737_2
----B737_3
-Boeing
----B747_1
-Cessna
----C182

As you see in my eg I will have now 2 parent nodes called Boeing, but since the B747_1 is also a
Boeing manufactured plane I would like to position this under the previous -boeing node toghteter with the
B737_... nodes. Anyway this method is something very new to me, and i'm thinkng of changing my other treeview control which is listes by installed folder the way you have shown me.
Thanks for this.
Johan

---Moderiert von user profile iconNarses: Beiträge zusammengefasst---

I think I found out why it isn't writing the childnodes under the parents.

Could it be due to following statements

ausblenden Delphi-Quelltext
1:
2:
  acft := TIniFile.Create(Aname);
  ManLookup := TStringList.Create;


So that for every loop it's performing recreates the both files and so looses the already created parentnodes?

When I put Manlookup declaration in the public section, also start the create during form.create, and use the free during form.close, then I have a working code. I hope this is still a good coding??

regards,
Johan
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10185
Erhaltene Danke: 1261

W11x64
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Di 08.09.09 13:07 
Moin!

user profile iconjcop hat folgendes geschrieben Zum zitierten Posting springen:
I think I found out why it isn't writing the childnodes under the parents.

Could it be due to following statements
ausblenden Delphi-Quelltext
1:
ManLookup := TStringList.Create;					

So that for every loop it's performing recreates the both files and so looses the already created parentnodes?
Bingo! ;)

user profile iconjcop hat folgendes geschrieben Zum zitierten Posting springen:
When I put Manlookup declaration in the public section, also start the create during form.create, and use the free during form.close, then I have a working code. I hope this is still a good coding?
As it seems to me, yes, I´d say so.

user profile iconjcop hat folgendes geschrieben Zum zitierten Posting springen:
This is much better (especially concerning fast loading),
You should consider then using the TMemIniFile class instead of TIniFile (which performs a lot of useless disk access). :idea:

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.