Add a radio and check box to the TreeView in Delphi

Source: Internet
Author: User

Turn on your computer, enter the Windows operating system, and clearly display information about all the disks that are managed by the system in the left column of the explorer, as well as the files and folders that each disk holds (figure I). This common display is made up of a root node and several child nodes, which is called a "tree structure." This kind of tree structure uses very widespread, in many commonly used software has appeared its figure. In Windows, this structure is encapsulated as a "tree control", the TreeView control, which, like ListView, Button, belongs to a common common control that is brought by the system. In Delphi, the TreeView is also encapsulated as a VCL component, which is located on the "Win32 component" panel and is one of the most commonly used components.

The TreeView component with Delphi can display a tree structure, or you can specify a different icon for each node to differentiate its functions. But in peacetime use, we find that it is not able to embed a checkbox or a RadioButton component, so that users can not visually select a subset of nodes or a node. How to solve this problem? When we think about it, we find that there are two ways to accomplish the tasks described earlier. One is to override a component by inheriting its functionality on the basis of the TreeView component and adding the desired functionality (so that the TreeView can embed a checkbox or a RadioButton component). The other is to use the user's illusion, the checkbox or the RadioButton can be implemented in two states of the picture (one is checked state another is unchecked) to alternate display, take the circuitous route to complete the task. Let's analyze the pros and cons of these two methods: the first method to rewrite a component, obviously more difficult, the time spent longer; the second method, using the TreeView component itself with the Display icon function, simple, short time to complete the requirements. After comparison, we choose the function of the second method, first look at the effect after the completion (such as figure II), should be said to achieve the goal, now we have to detail the completion of the process:

First, we select the ImageList component on the WIN32 panel, set its stateimages properties, including the two state icons, one is checked and the other is not in the first state.

Second, we call the Toggletreeview process (the implementation of the method see text), implementation of the mouse click and keyboard selection of the state icon to change the function.

The Toggletreeview process implementation code is as follows:

Procedure toggletreeviewcheckboxes (
Node:ttreenode;
cunchecked,//checkbox unchecked state
cchecked,//checkbox selected
cradiounchecked,//radiobuttion unchecked state
Cradiochecked : integer);  Radiobuttion the selected state
Var
Tmp:ttreenode
Begin
If assigned (Node) then
begin
//To be selected if it is currently unchecked
if Node.stateindex = cunchecked then
N Ode. Stateindex: = cchecked
//If it is currently selected it becomes unchecked
else if Node.stateindex = cchecked then
Node.stateindex: = Cun   Checked
Else If Node.stateindex = cradiounchecked then
begin
tmp: = node.parent;
If not assigned (TMP) THEN
tmp: = Ttreeview (Node.treeview).   Items.getfirstnode
Else
tmp: = Tmp.getfirstchild;
While assigned (TMP) does
begin
if (TMP). Stateindex in
[cradiounchecked,cradiochecked]) then
TMP. Stateindex: = cradiounchecked;
tmp: = tmp.getnextsibling;
End;
Node.stateindex: = cradiochecked;
End;//If Stateindex = cradiounchecked
End;//If Assigned (Node)
End;
Thirdly, the above code solves the problem of state icon conversion, so how do you change the state after mouse click and keyboard selection? The implementation code is given below:

Procedure Tform1.treeview1click (Sender:tobject);
Var
P:tpoint;
Begin
GetCursorPos (P); Get the position of the cursor
P: = Treeview1.screentoclient (P);
if (Htonstateicon in
Treeview1.gethittestinfoat (P.X,P.Y)) Then
Toggletreeviewcheckboxes (
Treeview1.selected,
Cflatuncheck,
Cflatchecked,
Cflatradiouncheck,
cflatradiochecked);
End

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.