Solved

TTreeView.items.data Problem

Posted on 1999-01-20
9
931 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

757 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

21 Experts available now in Live!

Get 1:1 Help Now