Solved

TTreeView.items.data Problem

Posted on 1999-01-20
9
1,012 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

749 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