Click here to Skip to main content
Click here to Skip to main content

Select only one node in Treeview

, 21 Aug 2011
Rate this:
Please Sign up or sign in to vote.
Select only one node in Treeview
If you want to check Treeview control's only one node for each root node, you can use this:
 
private void treeview1_AfterCheck(object sender, TreeViewEventArgs e)
{
    if (e.Node.Checked)
    {
        DiselectParentNodes(e.Node.Parent);
        DiselectChildNodes(e.Node.Nodes);
    }
}
 
private void DiselectParentNodes(TreeNode parent)
{
    while (parent != null)
    {
        if (parent.Checked)
            parent.Checked = false;
        parent = parent.Parent;
    }
}
 
private void DiselectChildNodes(TreeNodeCollection childes)
{
    foreach (TreeNode oneChild in childes)
    {
        if (oneChild.Checked)
            oneChild.Checked = false;
        DiselectChildNodes(oneChild.Nodes);
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Vano Maisuradze
Software Developer (Senior)
Georgia Georgia
No Biography provided

Comments and Discussions

 
GeneralThis code will uncheck all 'ancestor' nodes of a given TreeN... PinmemberBillWoodruff22-Aug-11 14:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 21 Aug 2011
Article Copyright 2011 by Vano Maisuradze
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid