This time, because to solve a technical problem, all day on the Internet run this run that check data. Because I mainly use Delphi as a development tool, so, the IT site Delphi Classification documents I also looked at, and harvested a lot. In those hundreds of documents, there are several documents that discuss the use of Ttreeview and TListView in the VCL library. When speaking of the TreeView, it is mainly about the method of achievement and the data structure. When speaking ListView, I mainly talked about supporting file dragging and so on. Today, I want to take ttreeview as an example, to talk about another aspect of the two VCL classes-the Data property of the TreeView or ListView item. Because I read the document, basically did not talk about the data property, mostly with an array or a list of strings to save the tree node users care about the data-such as the use of an array to hold the identification number of a data. In fact, the use of arrays or tstrings to save is not a good solution, as we all know, TreeNode and ListItem have a pointer type of data properties, flexible use of this pointer, than with arrays and tstrings much better. Of course, this is just a little trick, if you know the skills of friends, please don't laugh; a friend who often saves node data as an array or tstrings can look at it.
I illustrate with a more common department tree--we build the department tree and display the department name in the tree node, which displays the department's corresponding data (such as a list of people) when the user selects nodes in the tree. We know that because the department name is likely to repeat, so the department tree can not be positioned by the department name, only the department code to locate, so we want to save the department code in the tree node.
First, declare a structure to hold your node data:
Type
Tunitnodeinfo = Record
unitcode:string;
{In this record, you can also save some other data of interest to you, such as department manager ... }
End
Next, declare a pointer pointing to that structure:
Punitnodeinfo: ^tunitnodeinfo;
Then take a look at the processing of the achievements:
procedure CreateUnitTree;
var
TreeNode : TTreeNode;
NodeInof : PUnitNodeInfo;
begin
...
{在此的打开数据表、提取数据很多文档中有论及,我就不再重复}
TreeNoe := TreeView.Items.AddChild(...);
TreeNode.ImageIndex := ;{设置节点图标索引,当然也可以不在此设置而在TreeView的事件中设置}
New(NodeInof);{为节点指针分配内存}
with NodeInfo^ do
begin
UnitCode := ;{对节点数据属值}
......
end;{with...}
TreeNode.Data := NodeInof;
.....{上面代码放在复合语句中,增加树中每一个节点}
end;
Then, in the onchange event of the TreeView, you can get the node data in this way:
var
UnitCode : string;
begin
UnitCode :- TUnitNodeInfo(TreeView.Items[索引].Data^).UnitCode;
.........
end;
Finally, when you don't need a tree, remember to release the tree node pointer:
var
I : integer;
begin
for I := 0 to TreeView.Items.Count - 1 do
begin
Dispose(TreeView.Items[I].Data);
TreeView.Items[I].Data := nil;
end;
end;
The relative usage of ListView is similar to this, it is no longer spoken. Write these, also do not know can let you see understand, there is wrong place, please give more advice.