Solved

TTreeView.items.data Problem

Posted on 1999-01-20
9
960 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
 

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
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.

 
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

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

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…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

867 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

18 Experts available now in Live!

Get 1:1 Help Now