Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8093275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:28:13+00:00 2026-06-05T20:28:13+00:00

This is the feature i want to implement: check a tree node by click

  • 0

This is the feature i want to implement:

  1. check a tree node by click mouse as normal, user can multiple select.

  2. when user hold Ctrl+ mouse, i want the tree node which mouse clicked to be checked and all other nodes are unchecked.

I have a version but it is not elegant:

Any suggestions?

If you want to test the code, replace TreeViewAdv and TreeNodeAdv with normal TreeView and TreeNode.

using Syncfusion.Windows.Forms.Tools;

namespace treeviewtest
{
    public partial class Form1 : Form
    {
        TreeNodeAdv CurrentNode = null;
        public Form1()
        {
            InitializeComponent();
        }

        private void treeViewAdv1_AfterCheck(object sender, Syncfusion.Windows.Forms.Tools.TreeNodeAdvEventArgs e)
        {
            TreeViewAdv tree = sender as TreeViewAdv;
            if (Control.ModifierKeys == Keys.Control 
                  && e.Action!= TreeViewAdvAction.Unknown)
            {

                foreach (TreeNodeAdv node in tree.Nodes)
                {
                    if (node.CheckState == CheckState.Checked && node != CurrentNode)
                        node.Checked = false;

                    foreach (TreeNodeAdv n in node.WalkTree())
                    {
                        if (n.CheckState == CheckState.Checked && n != CurrentNode)
                            n.Checked = false;
                    }
                }
            }
            CurrentNode = null;
        }

        private void treeViewAdv1_BeforeCheck(object sender, TreeNodeAdvBeforeCheckEventArgs e)
        {
            if (Control.ModifierKeys == Keys.Control && CurrentNode == null)
            {
                CurrentNode = e.Node;
                if (e.NewCheckState == CheckState.Unchecked)
                {
                    TreeViewAdv tree = sender as TreeViewAdv;
                    foreach (TreeNodeAdv node in tree.Nodes)
                    {
                        if (node.CheckState == CheckState.Checked && node != CurrentNode)
                            node.Checked = false;

                        foreach (TreeNodeAdv n in node.WalkTree())
                        {
                            if (n.CheckState == CheckState.Checked && n != CurrentNode)
                                n.Checked = false;
                        }   
                    }
                    e.Cancel = true;
                }
            }
        }
    }

    public static class externtree
    {
        public static IEnumerable<TreeNodeAdv> WalkTree(this TreeNodeAdv node)
        {
            if (node == null)
                yield break;

            // return itself first
            yield return node;

            // return children
            if (node.HasChildren)
            {
                foreach (TreeNodeAdv cNode in node.Nodes)
                    foreach (TreeNodeAdv iNode in cNode.WalkTree())
                        yield return iNode;
            }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T20:28:15+00:00Added an answer on June 5, 2026 at 8:28 pm

    Make it elegant with inheritance and recursion:

    using System;
    using System.Windows.Forms;
    
    class MyTreeView : TreeView {
        protected override void OnAfterCheck(TreeViewEventArgs e) {
            if (checking) return;
            checking = true;
            if (e.Node.Checked && (Control.ModifierKeys & Keys.Control) == Keys.Control) {
                uncheckNodes(this.Nodes, e.Node);
            }
            checking = false;
            base.OnAfterCheck(e);
        }
    
        private void uncheckNodes(TreeNodeCollection nodes, TreeNode except) {
            foreach (TreeNode node in nodes) {
                if (node != except) node.Checked = false;
                uncheckNodes(node.Nodes, except);
            }
        }
        private bool checking;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

OK i want to implement the searching feature using mysql. Now to do this,
My client want to use Installing apps wirelessly feature. Using this he can download
I want to implement something like autocomplete feature like this: http://jqueryui.com/demos/autocomplete/ However I cannot
Suppose I have this feature branch foo. Now I want to merge it back
This feature worked a bit, then stopped working. Background: Given a user is logged
What is this feature in the attached image is called? Can i download a
Can we implement pull to refresh feature on a custom view controller?. My view
Can we implement pull to refresh feature on a custom view controller which has
I want to implement a sort of backup feature for my backup app, but
I want to add some feature to this project and I need smartgwt for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.