Solved

TTreeView.items.data Problem

Posted on 1999-01-20
9
976 Views
Last Modified: 2008-02-20
Hi all!

I've a problem with the items.data property of TTreeView.
I write the data-property as follow:
CNode.data:=PChar(IndexList[Index3]); (IndexList is a StringList)
How can I now read the string .data points to?
I've tried it with
MyString:=string(TreeView.Selected.Data);
but I always get nothing but junk in MyString
Please Help!

regards
rene
0
Comment
Question by:rene100
  • 2
  • 2
  • 2
  • +2
9 Comments
 
LVL 12

Expert Comment

by:rwilson032697
ID: 1362605
Thats because the data property is a PChar and not a string. An easier way is to assign the actual index of the string in the string list:

CNode.Data := Pointer(Index3);

then
 TheString := IndexList[integer(CNode.Data)];

Cheers;

Raymond.
0
 

Expert Comment

by:gdebiasa
ID: 1362606
As CNode.Data is only a pointer, if IndexList no longer exists, you will have nothing but junk on MyString.

I strongly recommend creating a TStringObj type (you'll have to define this) that just holds one string, and just work with objects, instead of trying to work with pointers.

You would create a new TStringObj and assign it to CNode.Data. Then, when you want to refer to it, just create a variable of type TStringObj and point it to TreeView.Selected.Data.

Don't forget to destroy these objects when you're finished.

Hope this helps,
Glen
0
 
LVL 12

Expert Comment

by:rwilson032697
ID: 1362607
Glen: The data property is a pointer for a fairly good reason. It so you can attach anything to it - not just objects.

In my experience when you have a treeview and you are attaching bits of data to it the treeview and that data are closely bound. That is to say the data is unlikely to be destroyed while the tree still points to it.

Creating an object as a wrapper for each string will create a lot of overhead as well duplicating all the strings...

Cheers,

Raymond.
0
ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 

Expert Comment

by:gdebiasa
ID: 1362608
Yes, you do have the overhead of having to destroy the objects associated with the TreeView (ditto for the ListBox, ListView, etc.).

With the right structures, it most certainly will work with pointers. I just found it easier to worry about destroying the objects, than having to deal with pointers (six in one, half dozen in the other I suppose).

I've also been using the OLO components from www.stonemicrodesigns.com, which automatically free objects from components like a TreeView, so it's been less of a programming hassle for me than for others.

With the power/size of machines today, I generally don't worry about the overhead of storing/duplicating strings. Yes, it may not be the most efficient mechanism, but for me, it works reliably, which is more important.

Glen
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1362609
If you just want to keep the way you planned it, use this:

MyString:=string(pchar(TreeView.Selected.Data));

But remember: this works only if the string (or in your case the string list) still exists...

Regards, Madshi.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1362610
HI rene100,

a little example (with a TListBox instead a TStringList) to handle the DataOption of a TreeNode


{The Data Record}
Type
  TPtrSPRec = ^TSPRec;
  TSPRec = Record
             s : String[100];
           end;

Var SP : TPtrSPRec; {Typed Pointer}


{A Function to get the Index of the
 First Selected Item in a ListBaox}
Function GetFirsSelectedItemIndex(LB : TListBox) : Integer;
var i, index : Integer;
begin
  i := 0;
  Index := -1;
  while (i < LB.Items.Count) and (index = -1) do
  begin
    If LB.Selected[i] then index := i;
    inc(i);
  end;
  Result := index;
end;


{Set NodeData}
procedure TForm1.BT_SetTDataClick(Sender: TObject);
Var selectedItemIndex : Integer;
begin
  selectedItemIndex := GetFirsSelectedItemIndex(ListBox1);
  If selectedItemIndex > -1 then  {If A ListBoxItem Selected}
  begin
    If treeview1.Selected.Data = Nil then
    begin                                     {No Data Stored}
      New(SP);
      SP^.s := ListBox1.Items[selectedItemIndex];
      treeview1.Selected.Data := SP;
    end
    else
    begin                                     {Change Stored Data}
      SP := treeview1.Selected.Data;
      SP^.s := ListBox1.Items[selectedItemIndex];
    end;
  end;
end;

{GetNodeData}
procedure TForm1.BT_GetTDataClick(Sender: TObject);
begin
  if treeview1.Selected.Data <> NIL then {Only if Data Stored}
  begin
    SP := treeview1.Selected.Data;
    edit1.text := SP^.s;
  end;
end;

{Free NodeData - A TreeViewEvent, which will be
 execute on NodeDelete for ever leaf ones and
 by Treeview.Destroy}
procedure TForm1.TreeView1Deletion(Sender: TObject; Node: TTreeNode);
begin
  If Node.Data <> Nil then  {Only if Data Stored}
  begin
    SP := Node.Data;
    Dispose(SP);
    Node.Data := Nil;
  end;
end;

{Delete selected Node}
procedure TForm1.BT_DelTNodeClick(Sender: TObject);
begin
  Treeview1.Selected.Delete;
end;

meikl
0
 
LVL 2

Author Comment

by:rene100
ID: 1362611
Thanks for your help!

I now use the solution of kretzschmar and it works fine.
Because he's also provided a good example, I will give him the points. So kretzschmar, please answer  the question again.

kind regards
rene
0
 
LVL 27

Accepted Solution

by:
kretzschmar earned 100 total points
ID: 1362612
hi rene100,

glad to helped you
thanks for the points and
happy programming again

meikl
0
 
LVL 2

Author Comment

by:rene100
ID: 1362613
Thanks again!
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

773 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