Click here to Skip to main content
15,909,737 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When a button is clicked (MoveBtn )How do I copy over the items selected in my tree view to a listbox.


private void MoveBtn_Click(object sender, EventArgs e)
{

    Listbox1.Items.Clear();
    foreach (var node in treeView1.Nodes)
    {
        Listbox1.Items.Add(node);
    }
    Listbox1.Show();

}

This code only shows the root, but not the child nodes which are selected. But I want to show the child nodes too -- if possible only the child nodes

What I have tried:

Listbox1.Items.Clear();
foreach (var node in treeView1.Nodes)
{
    Listbox1.Items.Add(node);
}
Listbox1.Show();
Posted
Updated 6-May-18 0:26am
v3
Comments
BillWoodruff 6-May-18 5:57am    
The standard WinForm TreeView does not support multiple selection: are you using a 3rd. party TreeView that does ? I doubt it, but I think asking the question is important, here, just in case.
codeprojectddx 6-May-18 10:49am    
when you use treeView1.Nodes ,you are getting those root notes .In fact the second level and below notes are not in the treeView1.Nodes.

In order to copy all the nodes you need a recursion. Consider the following example (sorry about the typos, I don't have a compiler at hand at the moment)

C#
public void Method1() {
   Listbox1.Items.Clear();
   AddNodes(treeView1.SelectedNode, Listbox1);
}

public void AddNodes(TreeNode parentNode, ListBox lb) {
   foreach (var node in parentNode.Nodes) {
      lb.Items.Add(node);
      AddNodes(node, lb);
   }
}
 
Share this answer
 
Comments
Member 13765884 5-May-18 17:11pm    
sorry, I dont really understand your answer.


private void MoveBtn_Click(object sender, EventArgs e)
{
Listbox1.Items.Clear();
AddNodes(treeView1.SelectedNode, Listbox1);
Listbox1.Show();
}

public void AddNodes(TreeNode parentNode, ListBox lb)
{
foreach (var node in parentNode.Nodes)
{
lb.Items.Add(node);
AddNodes(parentNode, lb);
}
}
I've tried it and am getting an error of


An unhandled exception of type 'System.NullReferenceException' occurred in monitoring and stock management.exe

Additional information: Object reference not set to an instance of an object.
Wendelius 5-May-18 17:51pm    
On which line do you get this exception?
Member 13765884 5-May-18 18:17pm    
foreach (var node in parentNode.Nodes)

is the line i get the error in
Wendelius 5-May-18 18:05pm    
You probably don't have a selected node when MoveBtn is clicked. Try the following
private void MoveBtn_Click(object sender, EventArgs e) {
   Listbox1.Items.Clear();
   if (treeView1.SelectedNode != null) {
      AddNodes(treeView1.SelectedNode, Listbox1);
   }
   Listbox1.Show();
}
Note: the Web Ms TreeView offers easy access to all checked TreeNodes: the WinForms TreeView TreeView does not.

So, you need a method to get all the TreeNodes, and then you need to select all the TreeNodes that are checked. This example uses the "classic" stack-based method that Eric Lippert advocates in order to avoid possible stack overflow with recursion:
public IEnumerable<TreeNode> GetNodes(TreeNodeCollection nodes)
{
    var stack = new Stack<TreeNode>();

    foreach (TreeNode node in nodes)
    {
       stack.Push(node);
    }

    while (stack.Count > 0)
    {
        var current = stack.Pop();
        yield return current;

        foreach (TreeNode child in current.Nodes)
        {
            stack.Push(child);
        }
    }
}
Then filter them to find the checked Nodes:
listBox1.Items.Clear();

var checkedNodes = GetNodes(treeView1.Nodes)
    .Where(nd => nd.Checked)
    .OrderBy(nd => nd.Text)  // remove if you don't want sorted 
    .ToArray();

listBox1.Items.AddRange(checkedNodes.ToArray());
 
Share this answer
 
v4
Comments
Member 13765884 6-May-18 10:10am    
Thank you, But this code only puts items which are selected (the highlighted node) into the listbox rather than putting all checked items into the listbox.
Do you know how to rectify it for checked items? - i cannot figure it out
BillWoodruff 6-May-18 12:36pm    
Now, finally, we know you are using CheckBoxes. See revised solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900