Delphi Treeview Operation Example

Source: Internet
Author: User

How do I change the icon in the TreeView? OnClick Event Click Get Node.text batch processing and implement the example of TreeView node dragging

This process chooses the icon according to your request
Procedure Tform1.treeview1getimageindex (Sender:tobject; Node:ttreenode);
Begin
If Node.haschildren Then
If Node.expanded Then
Node.imageindex: = 3//icon to open when node has child nodes
Else
Node.imageindex: = 0//icon to be collected when node has child nodes
else Node.imageindex: = 1; Icon when a node has no child nodes
End
This process displays the selected icon
Procedure Tform1.treeview1getselectedindex (Sender:tobject; Node:ttreenode);
Begin
Node.selectedindex: = Node.imageindex; icon to use after node selection
End

——————————————————————————————————-

The Click event for the TreeView
Procedure Tform1.treeview1click (Sender:tobject);
Begin
If the level of the selected node is 0 (the root node level is 0, the root node has a child node level of 1, and so on) and its ordinal (index) is 0 (the node in the same hierarchy presses the top-to-bottom number first is 0, the second is 1, and so on)
if (treeview1. Selected.level = 0) and (treeview1. Selected.index = 0) Then
Begin
Here's something to do after the first root node.
Form1. Color: = clred;
End
Else
if (treeview1. Selected.level = 1) and (treeview1. Selected.index=1) Then
Begin
Here's what to do after the 2nd child node in the third root node is written.
Form1. Color: = Clblue;
End
End
If you want the "+" number in front of the node to have the same effect, then you should write a judgment handler in the expanding event.
"Supplemental" "If you do not want to point the + number when the event is triggered, the following can be deleted"
Procedure tform1.treeview1expanding (Sender:tobject; Node:ttreenode;
var allowexpansion:boolean);
Begin
if (node. Level = 0) and (node. Index = 0) Then
Begin
Form1. Color: = clred;
End
Else
if (node. Level = 1) and (node. Index=1) Then
Begin
Form1. Color: = Clblue;
End
End

Direct Click Get Node.text

Procedure Tform1.treeview1click (Sender:tobject);
Begin
If TreeView1.Selected.Index >= 0 Then
Edit1.text: = TreeView1.Selected.Text;
End

————————————————————————————————————-

How to Batch process TreeView nodes
Use the BeginUpdate and EndUpdate methods of the Items property of the TreeView, for example:
TreeView1.items.BeginUpdate;
For i:=0 to Treeview1.items.count-1 do
Begin
File://Change the text of each node to a lowercase letter.
Treeview1.items[i].text:=lowercase (Treeview1.items[i].text);
End
TreeView1.items.EndUpdate;

————————————————————————————————————-

Implementing a TreeView Node Drag-and-drop instance
The following program fragment shows an example of how to implement dragging a TreeView widget node

{The statement executed when the mouse is pressed}
Procedure Tform1.treeview1mousedown (Sender:tobject;
Button:tmousebutton; Shift:tshiftstate; X, Y:integer);
Begin

{Left click and click on a node to start dragging}
if (Button = Mbleft) and
(Htonitem in Treeview1.gethittestinfoat (X, Y) and then
Begin
Treeview1.begindrag (False);
End
End

{Mouse drag to execute statement}
Procedure Tform1.treeview1dragover (Sender, Source:tobject;
X, Y:integer; State:tdragstate; var accept:boolean);
Var
Node:ttreenode;
Begin
If Source = Treeview1 Then
Begin
Node: = Treeview1.getnodeat (X, Y); {Fetch current node}
If node <> nil then {current node is not empty to implement drag, accept:=true}

Accept: = true;
End
End

{Statement executed when mouse is released}
Procedure Tform1.treeview1dragdrop (Sender, Source:tobject;
X, Y:integer);
Var
Tempnode:ttreenode;
Attachmode:tnodeattachmode;
Begin
If treeview1.selected = Nil Then
Exit;

Attachmode: = Naaddchild; {Set node move mode, set move node to child node}
{Note that there is a bug here, when the node is moved, if the target node has no child nodes,}
{The new child node that is added will fail, so the first is below the current target node}
{Join a temporary sub-node and delete the temporary node after the move is complete}

Treeview1.Items.BeginUpdate;
Try
Tempnode: = Treeview1.Items.AddChild (Treeview1.droptarget,
' Temp ');
Try
{Move the selected node to the target node}
Treeview1.Selected.MoveTo (Treeview1.droptarget, Attachmode);
Finally
Tempnode.free; {Don't forget to release the temporary node}
End
Finally
Treeview1.Items.EndUpdate;
End
End

Reference http://blog.csdn.net/delphi308/article/details/10124125

Delphi Treeview Operation Example

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.