Tips for getting checkbox selected items in Treeview under winform

Source: Internet
Author: User
Background

Today, I encountered some problems on the interface of a fingerprint management platform, because the company's basic library does not have operations on the winform platform, so in the scenario of selecting a department, it is difficult to obtain the selected project in the department Treeview. Fortunately, there is also an artifact called a search engine, So Google found that this problem under Asp.net is a good solution, but similar issues under the winform platform, such as rare!

Of course, today I have also checked for a long time and finally found two good articles, which helped me solve this problem and did not dare to exclusive. In combination with my own experiences on using this method, this document is specially recorded and shared with you through a small example.

The problem is:

There is a list of departments, multi-level (or infinitus). After we list them on the winform end (this is very simple), we need to select some of them and then click a button, submit the selected department to go to the next business operation scenario. The problem I encountered today is how to obtain the project selected at each level in the Treeview of this department? For example:

Here is just an example to illustrate, so let's just make it simple. As shown in the figure on the left, our task is to get the selected Department through a program and send the information to the next business scenario, in the traditional Asp.net. the Treeview control in the UI provides a convenient interface to obtain the selected project. However, when it comes to winform, the Instant Cup reminder ......

After I found that I could not use the Asp.net idea to do this, I immediately Googled and found the following article, which is very good and basically solved the problem I encountered. The original Article address is as follows:"Time-Space Tunnel"

I have summarized the general idea of the original author as follows:

Figure 1-simple flowchart

Solution:

Declare two variables on the page (form:

  • The first is used to store the selected objects. In this article, it is a stringbuilder object.
  • The second is a tag, which is"Tag is used in the Treeview node to append the checkbox selected ID information.", String object.

The code is described as follows:

String tag = "true"; // tag
Stringbuilder nodestag; // container

Next, we will start to process the aftercheck event so that the program can "remember" The project we selected. Whether you have read this article or not, you have mentioned "tree recursive algorithms". For details about algorithms, refer to the following link:Click to traverse

After learning about the algorithm, I believe everyone can leave home with the following code: copy the code of this Boyou here.

Traversal method

Private void traversnodes (treenode parent)
{
Treenode node = parent;
If (node! = NULL)
{
If (node. Tag! = NULL & node. Tag. tostring () = tag)
Nodestag. appendformat ("node: {0} has checked \ n", node. Text );

If (node. firstnode! = NULL) // If the node has sub-nodes, traverse
{
Traversnodes (node. firstnode );
}
If (node. nextnode! = NULL) // If the node is followed by a node of the same level, the traversal is performed.
{
Traversnodes (node. nextnode );
}
}
}

Aftercheck Event code:

Treeviewappsaftercheck

Private void treeviewappsaftercheck (Object sender, treevieweventargs E)
{Www.elivn.com
If (E. Action! = Treeviewaction. Unknown)
{
Treenode node = E. node;
If (node. Tag = NULL)
Node. Tag = tag; // additional node Information
Else
Node. Tag = NULL;

Checkallchildnodes (E. node, E. node. Checked );

// Select the parent node
Bool Bol = true;
If (E. node. parent! = NULL)
{
For (INT I = 0; I <E. node. Parent. nodes. Count; I ++)
{
If (! E. node. Parent. nodes [I]. Checked)
BOL = false;
}
E. node. Parent. Checked = Bol;

//// Remember to set its Tag if the parent node is selected or canceled.
If (BOL)
{
E. node. Parent. Tag = tag;
}
Else
{
E. node. Parent. Tag = NULL;
}
}
}
}

When should this method be called? In my scenario, we should click the business button again. Before that, the only thing we had to do with the aftercheck Event code was the process shown in:

After clicking the business button, add the following code for it:

Call Method

Private void button1_click_1 (Object sender, eventargs E)
{
Nodestag = new stringbuilder ();
Treenode node = treeview1.topnode; // obtain the root node of the Treeview. Note that there is only one root node.
// Each time the root node is searched
While (node. prevnode! = NULL)
Node = node. prevnode;
Traversnodes (node); // traverses the root node
MessageBox. Show (nodestag. tostring ());
}
Even better:

Now, the problem has been solved. We have obtained the selected department. However, as a general rule, we should select all the subnodes if there are subnodes in the selected node; if you cancel the selected state of a node and the current node has a parent node, the selected state of the parent node will be canceled. "What should I do?

Well, according to the original article, we can combine the previous code to get the selected status and modify it as follows:

New aftercheck

Private void treeviewappsaftercheck (Object sender, treevieweventargs E)
{
If (E. Action! = Treeviewaction. Unknown)
{
Treenode node = E. node;
If (node. Tag = NULL)
Node. Tag = tag; // additional node Information
Else
Node. Tag = NULL;

Checkallchildnodes (E. node, E. node. Checked );

// Select the parent node
Bool Bol = true;
If (E. node. parent! = NULL)
{
For (INT I = 0; I <E. node. Parent. nodes. Count; I ++)
{
If (! E. node. Parent. nodes [I]. Checked)
BOL = false;
}
E. node. Parent. Checked = Bol;

//// Remember to set its Tag if the parent node is selected or canceled.
If (BOL)
{
E. node. Parent. Tag = tag;
}
Else
{
E. node. Parent. Tag = NULL;
}
}
}
}

The new checkallchildnodes method:

As prompted by Boyou flynn2009, you should note that the code needs to be modified as follows:

Node. Tag = nodechecked? (Bool) Tag: NULL;

New checkallchildnodes

Private void checkallchildnodes (treenode, bool nodechecked)
{
Foreach (treenode node in treenode. nodes)
{
Node. Checked = nodechecked;
Node. Tag = tag; // remember to set the tag attribute for the selected project.
If (node. nodes. Count> 0)
{
This. checkallchildnodes (node, nodechecked );
}
}
}
End:

Well, here we have completed a small example of getting the checkbox selected status in the Treeview, and we have also set a user-friendly effect on the parent and child options. This is the end of the article. I hope it will bring you some benefits. Welcome !!!

The sample project code of this article is attached: vs2008 +. NET 2.0

Click to get

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.