Check the number of checked nodes in TreeView

I am new to using tree views, and I want to make sure that there can only be one child node in the tree view, and if someone tries to check more than one, it stops the validation event and deselects all the parent and child nodes. How can I do it? So far this is what I have, but it's freaky. Any suggestions?

Update: To clarify some things, this is a treeview of the win form, and the parent node is a category, and each category can contain multiple elements. I just 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;
            }
        }
    }

After some confusion, I realized what solution I got. I 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();
            }
        }
    }
}
0
4

.

, Windows Forms TreeView: ?

:

- .

, treeview, , Windows Forms TreeView . " ", : , CodeProject: "Multiselect Treeview Implementation" , .

, , "" : : , , , , TreeNode: , , " " .

, , , node , , "" (root) node.

, , :

  • (root) node?

  • (root) node?

  • (root) node, , node ?

  • " ": - ?

: - WinForms TreeView . TreeView :

UI TreeView Lidor Systems . Lidor TreeView CheckedNodes, , , "MultiExtended", , , "":

, , SelectedNodes, , .

Lidor TreeView CheckBox node ( ), CheckBox : ", " checked "" unchecked ".

, . TreeView , " " , Lidor TreeView . "" TreeNodes, Lidor TreeView "WinForms Universe". : : , .

+1

if (checkedNodeCount > 2)... foreach.

for . , , " "

, , .

+1

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;
                }
            }
        }
+1
source

It was asked 1 year ago, but there is a hint there, how to select only one node in Treeview

0
source

Source: https://habr.com/ru/post/1727439/


All Articles