Solved

TTreeView.items.data Problem

Posted on 1999-01-20
9
1,038 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: 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

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!

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

691 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