From: http://blog.csdn.net/am2004/archive/2007/05/22/1621349.aspx
Origin:A tree control (Treeview) is used to set permissions for a winform project recently ). When the checkboxes attribute of Treeview is set to true, a selection box appears before each node of the tree control. However, the selection box only has two display appearances: checked (checked in the box) and unselected. I believe many of my friends also hope to have a third display appearance, which will be used when the child node of the node is selected. You are advised to develop custom controls inherited from the Treeview by yourself and extend attributes in the custom controls to achieve three states. I will not discuss the advantages and disadvantages of the solution. Can other solutions be implemented quickly?
Ideas:Treeview has the imagelist and imageindex attributes, which can be used to define the displayed image for each node. If we bring our own display images in three states, the control node should display that image in the nodemouseclick event. Is that OK? The answer is yes, and it is not complicated.
One of the key points:You need to set the checkboxes attribute of Treeview to false, prepare three images (image address: http://album.pixplayer.com/208003_242/), we use imageindex to represent the node 0, 1, 2 respectively
Unselected, selected, and subnode selectedThe checked attribute of a node is not considered. Final piece (image address: http://album.pixplayer.com/208003_242)
Key Point 2:Before programming, we need to determine the processing rules of any node when it is clicked (nodemouseclick event). After the rules are clear, the program will basically write them out.
// Rule 1: unselect // rule 1.1: Check whether a child node exists. You need to clear the selected status of all child nodes. // Rule 1.2: Check whether a parent node exists. If yes, modify the selected status of the parent node according to the selected status of the sibling node // Rule 2: Select // rule 2.1: Check whether there are child nodes, set all child nodes to the selected status // rule 2.2: Check whether a parent node exists. If yes, modify the selected status of the parent node based on the selected status of the sibling node. |
Code reference:
// Set Private void buttonstree_nodemouseclick (Object sender, treenodemouseclickeventargs E) for the image displayed after the selection and cancellation. {// Rule 1: deselect the selection // rule 1.1: Check whether a subnode exists, you need to clear the selected status of all child nodes. // Rule 1.2: Check whether a parent node exists. If yes, modify the selected status of the parent node according to the selected status of the sibling node // Rule 2: Select // rule 2.1: Check whether there are child nodes, set all child nodes to the selected status // rule 2.2: Check whether a parent node exists. If yes, modify the selected status of the parent node if (E. node. imageindex = 1) {e. node. selectedimageindex = 0; E. node. imageindex = 0; setnodeimg11 (E. node); setnodeimg12 (E. node );} Else {e. node. selectedimageindex = 1; E. node. imageindex = 1; setnodeimg21 (E. node); setnodeimg22 (E. node) ;}} // set the node selection status: rule. 1: Check whether a subnode exists. You need to clear the selected status of all subnodes. Private void setnodeimg11 (treenode Tn) {foreach (treenode t in TN. nodes) {T. selectedimageindex = 0; T. imageindex = 0; If (T. nodes. count! = 0) {setnodeimg11 (t) ;}}// set the node selection status: rule. 2: Check whether a parent node exists. If yes, modify the selected private void setnodeimg12 (treenode Tn) {If (TN. parent = NULL) return; int imenode num = 0; int img1num = 0; int img2num = 0; // count the foreach (treenode t in TN) selected by the sibling node. parent. nodes) {If (T. imageindex = 0) im1_num ++; If (T. imageindex = 1) img1num ++; If (T. imageindex = 2) img2num ++;} // if there is an IF (img2num! = 0) | (im1_num! = 0) & (img1num! = 0) {tn. parent. selectedimageindex = 2; tn. parent. imageindex = 2;} else {tn. parent. stateimageindex = 0; tn. parent. imageindex = 0;} // recursive setnodeimg12 (TN. parent);} // set the node selection status: rule. 1: Check whether there are subnodes. Set all subnodes to the selected private void setnodeimg21 (treenode Tn) {foreach (treenode t in TN. nodes) {T. selectedimageindex = 1; T. imageindex = 1; if (T. nodes. count! = 0) {setnodeimg21 (t) ;}}// set the node selection status: rule. 2: Check whether a parent node exists. If yes, modify the selected private void setnodeimg22 (treenode Tn) {If (TN. parent = NULL) return; int imenode num = 0; int img1num = 0; int img2num = 0; // count the foreach (treenode t in TN) selected by the sibling node. parent. nodes) {If (T. imageindex = 0) im1_num ++; If (T. imageindex = 1) img1num ++; If (T. imageindex = 2) img2num ++;} // if there is an IF (img2num! = 0) | (im1_num! = 0) & (img1num! = 0) {tn. parent. selectedimageindex = 2; tn. parent. imageindex = 2;} else if (img1num = 0) & (img2num = 0) {tn. parent. selectedimageindex = 0; tn. parent. imageindex = 0;} else {tn. parent. stateimageindex = 1; tn. parent. imageindex = 1;} // recursive setnodeimg22 (TN. parent );} |