tags:

views:

1406

answers:

3

I am new to using tree views and I want to be able to make sure the tree view can only have one child node checked and if someone tries to check more then one it stops the check event and deselects all parent and child nodes. How would I go about doing this? So far this is what I've got but it is acting quirky. Any suggestions?

EDIT: To clarify some things this is a win form treeview and the parent node is a category and each category can contain multiple items. I only want the user to be able to select one category and one item from the category at a time.

private void tvRecipes_BeforeCheck(object sender, TreeViewCancelEventArgs e)
    {
        int checkedNodeCount = 0;

        if (e.Node.Parent != null && !e.Node.Parent.Checked)
            e.Cancel = true;
        else
        {
            foreach (TreeNode node in tvRecipes.Nodes)
            {
                if (node.Checked)
                    ++checkedNodeCount;

                if (checkedNodeCount > 2)
                {
                    MessageBox.Show("Only one recipe can be selected at a time, please deselect the current recipe and try again.", "Too Many Recipes Selected", MessageBoxButtons.OK, MessageBoxIcon.Error);

                    e.Cancel = true;
                }
            }
        }
    }

Thanks.

After some messing around I figured out the solution I was after. I have posted it below:

private bool CheckNumOfSelectedChildern(TreeViewEventArgs e)
    {
        bool Valid = true;
        int selectedChildern = 0;

        foreach (TreeNode node in tvRecipes.Nodes)
        {
            if (node.Checked)
            {
                foreach (TreeNode child in node.Nodes)
                {
                    if (child.Checked)
                        ++selectedChildern;
                }
            }
        }

        if (selectedChildern > 1)
        {
            MessageBox.Show("Only one recipe per category can be selected at a time, please deselect the current recipe and try again.", "Too Many Recipes Selected", MessageBoxButtons.OK, MessageBoxIcon.Error);
            e.Node.Checked = false;
            e.Node.Parent.Checked = false;
            Valid = false;
        }

        return Valid;
    }

private bool CheckNumOfSelectedParents(TreeViewEventArgs e)
    {
        bool Valid = true;
        int selectedParent = 0;

        foreach (TreeNode root in tvRecipes.Nodes)
        {
            if (root.Checked)
                ++selectedParent;
        }

        if (selectedParent > 1)
        {
            MessageBox.Show("Only one recipe category can be selected at a time, please deselect the current recipe and try again.", "Too Many Recipes Selected", MessageBoxButtons.OK, MessageBoxIcon.Error);
            e.Node.Checked = false;
            Valid = false;
        }

        return Valid;
    }

private void tvRecipes_BeforeCheck(object sender, TreeViewCancelEventArgs e)
    {
        if (e.Node.Parent != null && !e.Node.Parent.Checked)
            e.Cancel = true;
        else if (e.Node.Checked)
        {
            foreach (TreeNode child in e.Node.Nodes)
            {
                if (child.Checked)
                    e.Cancel = true;
            }
        }
    }

 private void tvRecipes_AfterCheck(object sender, TreeViewEventArgs e)
    {
        if (CheckNumOfSelectedParents(e))
        {
            if (e.Node.Parent != null && e.Node.Parent.Checked)
            {
                if (e.Node.Checked)
                {
                    if (CheckNumOfSelectedChildern(e))
                    {
                        RecipeDs = RetrieveRecipe.FillRecipeDs(e.Node.Text);
                        DataBindControls();
                    }                    
                }
                else
                {
                    RemoveLabelsFromLayout();
                    RemoveDataBindings();
                    RecipeDs.Clear();

                    this.Refresh();
                }
            }
        }
    }
+2  A: 

This is too long for just a comment.

I assume you are describing the standard Windows Forms TreeView : correct ?

When you say in your question :

if someone tries to check more then one it stops the check event and deselects all parent and child nodes.

To me that implies you have a treeview capable of multiple selection, and the standard Windows Forms TreeView is not capable of this. You can "hack it" so it does multi-selection : for example : CodeProject : "Multiselect Treeview Implementation", but it's non-trivial to do so.

So I am going to assume you meant "uncheck" parent and child nodes : but then : that is confusing because your question appears to ask clearly how you can have only one child TreeNode checked at one time : that implies to me that it would never be the case that you would have to "uncheck" any other child nodes.

Reading your code, it's obvious that you are planning for the possibility that one parent node is checked, but you do not specifically exclude the case that more than one "parentless" (root) node could be checked.

So, suggest re-stating your question a little more clearly to clarify :

  1. more than one parentless (root) node can be checked ?

  2. only one parentless (root) node can be checked at one time ?

  3. for each parentless (root) node checked, one, and only one, child node can be checked at any one time ?

  4. if child nodes are "deeply nested" : does that make any difference to you ?

Note : because of the great limitations of the native WinForms TreeView control, a lot of us have purchased 3rd. party TreeView controls :

I purchased the Integral UI TreeView from Lidor Systems, and have been delighted with it. A built-in property of the Lidor TreeView is the 'CheckedNodes collection containing all currently checked nodes, and you have your choice of three selection modes, including one "MultiExtended" that allows you to have very complex selections that include child and parent nodes in any order, at any level of "nesting" :

And of course you have a 'SelectedNodes collection available at any time containing all selected Nodes.

With the Lidor TreeView you can hide the CheckBox for any one Node at any time (at design time, or at run-time), and the CheckBox supports three states : 'indeterminate as well as 'checked and 'unchecked.

I assume other 3rd. party TreeView controls also offer similar features, but when I reviewed and tried "trial versions" of most of the major ones two years, ago : for me the Lidor TreeView offered the most value and was not expensive. In terms of the ability to richly "style" TreeNodes, I didn't see anything close to the capability of the Lidor TreeView in the "WinForms Universe." Disclosure : I do not work for Lidor, and have no business relationship with them : I'm just a happy customer who's pleased to recommend.

BillW
+1  A: 

Your if (checkedNodeCount > 2)... should be outside the foreach loop.

Inside the for loop, it gets run each time. I'm assuming this is what you mean by "acting quirky"

Perhaps you should give some more detail about what the incorrect behavior is.

Neil N
+1  A: 

Hope this works:

private int _callCountUp;

        private int _callCountDn;

private void tvwPermissions_AfterCheck(object sender, System.Windows.Forms.TreeViewEventArgs e)
        {
            bool anyChecked = false;

            if (_callCountDn == 0 && e.Node.Parent != null)
            {
                anyChecked = false;
                foreach (TreeNode childNode in e.Node.Parent.Nodes)
                {
                    if (childNode.Checked)
                    {
                        anyChecked = true;
                        break;
                    }
                }
                _callCountUp += 1;

                if (anyChecked)
                    e.Node.Parent.Checked = true;

                _callCountUp -= 1;
            }

            if (_callCountUp == 0)
            {
                foreach (TreeNode childNode in e.Node.Nodes)
                {
                    _callCountDn += 1;
                    childNode.Checked = e.Node.Checked;
                    _callCountDn -= 1;
                }
            }
        }
JMSA