TreeView uses notes

zhaozj2021-02-11  170

TreeView uses notes

TreeView is composed of nodes, and the build tree operates through the TreeView.Items property. Items is a TTREENODES object, this is a TTREENODE set.

First, targeting TTreenodes, TreeView.Items, with these properties: 1, count, node. 2, item [index], get the node via index.

Second, for TTREENODES, that is, TreeView.Items, the operation of the commonly used node is: AddFirst Add the first root node. The node in which the node added by this function is in front, unless it has been used to add a node, then the node added is ranked in front. This function returns a newly added node. AddChildFirst adds the first child node that requires a parent node as its parameters. Returns the newly added node. AddChild adds a child node that requires a parent node as its parameters. Returns the newly added node. Add Add a brother node and requires a brother node as its parameters. Returns the newly added node.

Third, targeting TTREENODES, TreeView.Items, commonly used access to the node: getFirstNode () gets the root node. Then cooperate with TTREENODE.GETNEXT (), you can access all nodes.

Fourth, the construction tree example:

var root_node, cur_node: TTreeNode; begin root_node: = AddFirst (nil, 'root 1'); cur_node: = addChildfirst (root_node, nil, 'root 1_child1'); add (cur_node, 'root 1_child2'); root_node : = Add (nil, 'root node 2'); addChildfirst (root_node, '' root node 2_child1 ');

V. Event Trigger: When jumping from a node to another, TtreeView.Onchange event is triggered. In this event, Node will pass, that is, the node currently selected.

When modifying the text of a node, TtreeView.onEdit events are triggered.

6. Connect the data corresponding to the node and node for each TTREENODE, with a DATA property, can store a pointer. We can use this domain to store their own data corresponding to the node. 1. Let's define a data structure as a record that records the data we want to record. Such as: Type PMYDATA = ^ TMYDATA; TMYDATA = Record Sfname: String; Slname: String; NINDEX: Integer;

2. Then, when the number is created, connect the node and node data: procedure tform1.button1click (sender: Tobject); var myshuju: PMYDATA CUR_NODE: TTREENODE; begin new (myRecptr); // Remember, must assign memory first . There are several nodes that assign several memory. myshuju ^ .FName: = Edit1.Text; Myshuju ^ .LName: = Edit2.Text; TreeViewIndex: = StrToInt (Edit3.Text); with TreeView1 do begin cur_node: = items.AddFirst (nil, 'first'); cur_node. Data: = myshuju; end;

3. When we choose a node, we can use our data. procedure TForm1.TreeView1Change (Sender: TObject; Node: TTreeNode); begin if node.data <> nil then self.label1.caption: = pmyData (node.data) ^ Fname pmyData (node.data) ^ Lnameend;.. Seven, general usage process: 1. Add global variable: b_first: boolean; // Record whether it is the first access node, because the data is not ready, and once the access node will trigger an onchange event, in this event handle There may be an error in the function. 2, in FormCreate, A, set B_First: = true; b. Create a number and contact the node to the data. 3. Set B_First: = FALSE; 4. Handle the node in the event onchange. 5. Handling the node in the EDIT to modify the Text event. And call onchange.6. Release the memory space points to the DATA in the TreeView.Destory.

转载请注明原文地址:https://www.9cbs.com/read-5946.html

New Post(0)