c # TreeView achieve three selected

c # projects need to achieve checked the tree options, a full election, half tick, do not choose the state.

Because only the original check and control the two states are not checked, so the half tick status codes need to draw and self-defined.

Check: Node.Checked = true

Uncheck: Node.Checked = false

Semi checked: Node.Checked = false && Node.ToolTipText = "section check"

Note: Check the semi-state needs to redraw the control you need to set control properties DrawMode to OwnerDrawText or OwnerDrawAll

A core logic: setting the selected parent node (a node when selected, it is determined whether or not select all sibling nodes; canceled when a node needs to judge whether the sibling node has selected a half-selection state and the selected state is determined thereby to the parent node. )

 // After canceling selected node, the parent node to cancel all selected 
        Private  void setParentNodeCheckedState (currNode the TreeNode, BOOL State) 
        { 
            the TreeNode the parentNode = currNode.Parent; // get the parent node of the current node 
            parentNode.Checked = State; // Set selected parent node
             iF (state == to false ) // when the status is set to false 
            { 
                int in Flag = 0 ;
                 the foreach (broNode the TreeNode in parentNode.Nodes) / / child nodes of the parent node is determined, a check whether there is a half options states 
                { 
                    IF(broNode.Checked || broNode.ToolTipText.Equals ( " Part tick " )) 
                    { 
                        In Flag = . 1 ; 
                    } 
                } 
                IF (In Flag == . 1 ) // set parent node has a semi-checked state 
                { 
                    parentNode.ToolTipText = " partially checked " ; 
                    parentNode.Checked = to false ; 
                } 
                the else   
                {    // Sets the parent node status is not checked 
                    parentNode.ToolTipText ="" ; 
                    ParentNode.Checked = to true ; 
                    parentNode.Checked = to false ;   // Checked need to change the state of the node, in order to redraw the control; 
                } 
            } 
            the else {    // if the parent node is set to the selected state
                 int In Flag = 0 ;
                 the foreach ( broNode TreeNode in parentNode.Nodes) // determine whether there is unselected node under the child node of the parent node. 
                { 
                    IF (! BroNode.Checked) 
                    { 
                        In Flag = . 1 ;
                    }
                } 
                IF (In Flag == . 1 ) 
                { 
                    parentNode.ToolTipText = " section check " ; 
                    parentNode.Checked = to false ; 
                } 
                the else 
                { 
                    parentNode.ToolTipText = "" ; 
                    parentNode.Checked = to true ; 
                } 
            } 
            IF (currNode.Parent.Parent ! = null ) // if there is a parent node on the parent node
            {  
                setParentNodeCheckedState (currNode.Parent, State); // recursive call 
            } 
        }

Two core logic: setting selected subnode

 // After the selected node, all child nodes selected nodes 
        Private  void setChildNodeCheckedState (currNode the TreeNode, BOOL State) 
        { 
            TreeNodeCollection Nodes = currNode.Nodes; // get all child nodes
             IF (nodes.Count> 0 ) // child node exists
                 foreach (TN the TreeNode in nodes) 
                { 
                    tn.Checked = State; 
                    tn.ToolTipText = "" ; 
                    setChildNodeCheckedState (TN, State); child node // recursive call to child nodes 
                } 
        }

Set the control monitor property AfterCheck

  Private  void skinTreeView1_AfterCheck ( Object SENDER, TreeViewEventArgs E) 
        { 
            IF (e.Action == TreeViewAction.ByMouse) // mouse click 
            { 
                // textBox1.Text = e.Node.Text; 
                IF (e.Node.Checked) // check 
                {                     e.Node.ToolTipText = "" ;
                     // after the selected node, all child nodes selected nodes 
                    setChildNodeCheckedState (e.Node, to true );
                     IF (! e.Node.Parent = null ) 
                    {
                        setParentNodeCheckedState (e.Node, to true ); 
                    } 
                }  
                the else   // unchecked 
                { 
                    e.Node.ToolTipText = "" ;
                     // After canceling node selected, cancel all child nodes selected 
                    setChildNodeCheckedState (e.Node, to false );
                     // If the node is a parent node exists, the parent node of the deselected 
                    IF (e.Node.Parent =! null ) 
                    { 
                        setParentNodeCheckedState (e.Node, to false ); 
                    }
                }
            }
        }

Provided redraw monitor DrawNode, draw a new pattern is a semi-checked state

  Private  void ClassTreeList_DrawNode ( Object SENDER, DrawTreeNodeEventArgs E) 
        { 
            IF (e.Bounds.Location.X <= 0 ) 
            { 
                return ; 
            } 
            var TreeView = SENDER AS the TreeView;
             var Brush = Brushes.Black; // black
             IF (e.Node .ToolTipText.EndsWith ( " part tick " ) && == e.Node.Checked to false ) 
            {   // check the state of semi-determined
                 var LOCATION = e.Node.Bounds.Location;
                location.Offset ( - 10 , . 7 );
                 var size = new new Size ( . 7 , . 7 ); 
                e.Graphics.FillRectangle (Brush, new new the Rectangle (LOCATION, size)); // draw a square here 
            } 
            // draw the text 
            e.Graphics.DrawString (e.Node.Text, treeview.Font, Brush, e.Bounds.Left, e.Bounds.Top); 
        }

 

Guess you like

Origin www.cnblogs.com/yhood/p/11532787.html