In Windows Forms, add the selected Treeview value to ListBox.

Source: Internet
Author: User

1: layer-2 data in the Treeview. After the parent node is selected, all child nodes are automatically selected.

2: add layer 2 data to ListBox

3: if the data already exists in ListBox, do not add

4: You can delete the selected data in ListBox.

Class: treeviewcheck is used to automatically select all child nodes after the parent node is selected.

Code

 1 public static class treeviewcheck
2 {
3 /// <summary>
4 // series node checked attribute Control
5 /// </Summary>
6 /// <Param name = "E"> </param>
7 public static void checkcontrol (treevieweventargs E)
8 {
9 If (E. Action! = Treeviewaction. Unknown)
10 {
11 if (E. node! = NULL)
12 {
13 checkparentnode (E. node, E. node. Checked );
14}
15 if (E. node. nodes. Count> 0)
16 {
17 checkallchildnodes (E. node, E. node. Checked );
18}
19}
20}
21 # region private Method
22 // change the status of all sub-nodes
23 Private Static void checkallchildnodes (treenode Pn, bool ischecked)
24 {
25 foreach (treenode tn in PN. nodes)
26 {
27 tn. Checked = ischecked;
28 If (TN. nodes. Count> 0)
29 {
30 checkallchildnodes (TN, ischecked );
31}
32}
33}
34 // change the selected status of the parent node
35 Private Static void checkparentnode (treenode curnode, bool ischecked)
36 {
37 bool bchecked = true;
38 If (curnode. parent! = NULL)
39 {
40 foreach (treenode node in curnode. Parent. nodes)
41 {
42 if (node. Checked = false)
43 {
44 bchecked = false;
45 break;
46}
47}
48 if (bchecked)
49 {
50 curnode. Parent. Checked = true;
51 checkparentnode (curnode. Parent, true );
52}
53 else
54 {
55 curnode. Parent. Checked = false;
56 checkparentnode (curnode. Parent, false );
57}
58}
59}
60 # endregion
61}

Node selection or cancellation in Treeview:

 

    private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
{
TreeViewCheck.CheckControl(e);

}

 

 

Add the value of the selected item in the Treeview to ListBox

Code

 Private void button#click (Object sender, eventargs E)
{

Foreach (treenode node in treeview1.nodes)
{

Foreach (treenode nd in node. nodes)
{
If (Nd. Checked)
{
If (verifynotexist (Nd. Text ))
{
Listbox1.items. Add (Nd. Text );
}
}
}
}
}

Public bool verifynotexist (string thenodetext)
{
// Facilitates the project in listbox1. If the project already exists, false is returned.
For (INT I = 0; I <listbox1.items. Count; I ++)
{
If (thenodetext = listbox1.items [I]. tostring ())
{
Return false;
}
}
// Returns true if it does not exist.
Return true;
}

 

// Delete the selected item

Code

    //ListBox.SelectedIndexCollection indices = listBox1.SelectedIndices;  

// int count = indices.Count;

// listBox1.BeginUpdate();

// for (int i = 0; count != 0; i++)

// {
// listBox1.Items.RemoveAt(indices[0]);

// count--;
// }

//listBox1.EndUpdate();

while (listBox1.SelectedIndex != -1)
{

listBox1.Items.Remove(listBox1.SelectedItem);

}

 

 

 

Delete all items in ListBox

  listBox1.Items.Clear();

 

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.