Click here to Skip to main content
15,890,506 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

Assume that we have two Listboxes, one includes Departments and the second includes all the people in a company.

Now, when I select a department from lstDept listbox and some people from lstPeople listbox, I want to show the selected items in a treeview or something else (with a good layout).

Does anybody knows how can I show that in a good layout?

Thank you.
Posted
Updated 6-Dec-11 3:38am
v3

Asp.Net Listbox and Tree View Code Updated:
UI:
XML
<table>
    <tr>
    <td> <asp:ListBox ID="listBox1" runat="server" Height="192px"
        onselectedindexchanged="ListBox1_SelectedIndexChanged" Width="147px"
            AutoPostBack="True"></asp:ListBox></td>
    <td> <asp:ListBox ID="listBox2" runat="server" Height="190px"
        onselectedindexchanged="ListBox2_SelectedIndexChanged" Width="142px"
            AutoPostBack="True"></asp:ListBox></td>
    <td> <asp:TreeView ID="TreeView1" runat="server">
    </asp:TreeView></td>
    </tr>
    </table>

Csharp Code:

C#
  protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            listBox1.Items.Add("Development");
            listBox1.Items.Add("Testing");
            listBox1.Items.Add("HR");
            listBox1.Items.Add("Admin");
            listBox1.Items.Add("External Affairs");
            listBox2.Items.Add("EmpIDone");
            listBox2.Items.Add("EMpIDTWo");
            listBox2.Items.Add("EMptree");
        }

    }

protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
   {

       foreach (TreeNode node in TreeView1.Nodes)
       {
           if (node.Text == listBox1.SelectedItem.Text)
               isExists = true;
       }
       if (!isExists)
       {
           TreeView1.Nodes.Add(new TreeNode(listBox1.SelectedItem.Text));
       }
   }
   protected void ListBox2_SelectedIndexChanged(object sender, EventArgs e)
   {
       foreach (TreeNode node in TreeView1.Nodes)
       {
           if (node.Text == listBox1.SelectedItem.ToString())
           {
               TreeNode currentnode = node;
               currentnode.ChildNodes.Add(new TreeNode(listBox2.SelectedItem.Text));
           }
       }

   }
 
Share this answer
 
Comments
enginbilici 7-Dec-11 3:33am    
Thank you very much Saichandra, It worked successfuly. But there is a little problem now, It adds same person to child nodes even there is same person already. But I think I can handle it, thank you soo much again.
try this code:

C#
private void Form1_Load(object sender, EventArgs e)
        {
            listBox1.Items.Add("Development");
            listBox1.Items.Add("Testing");
            listBox1.Items.Add("HR");
            listBox1.Items.Add("Admin");
            listBox1.Items.Add("External Affairs");
            listBox2.Items.Add("EmpIDone");
            listBox2.Items.Add("EMpIDTWo");
            listBox2.Items.Add("EMptree");
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {


            bool isExists = false;

            foreach (TreeNode node in treeView1.Nodes)
            {
                if (node.Text == listBox1.SelectedItem.ToString())
                {
                    isExists = true;
                    return;
                }
            }

            if (!isExists)
            {
                treeView1.Nodes.Add(listBox1.SelectedItem.ToString());
            }

        }

        private void listBox2_Click(object sender, EventArgs e)
        {
            foreach (TreeNode node in treeView1.Nodes)
            {
                if (node.Text == listBox1.SelectedItem.ToString())
                {
                    TreeNode currentnode = node;
                    currentnode.Nodes.Add(listBox2.SelectedItem.ToString());
                }
            }
        }
 
Share this answer
 
Comments
enginbilici 6-Dec-11 11:55am    
Hi, first of all thank you for your interest.
treeView1.Nodes.Add(listBox1.SelectedItem.ToString()); ---> this line has throw an error and I have changed it to
-------
TreeNode node = new TreeNode();
node.Text = listBox1.SelectedItem.ToString();
TreeView1.Nodes.Add(node);
-------


TreeNode currentnode = node;
currentnode.Nodes.Add(listBox2.SelectedItem.ToString()); ---> this line has an error too but I could not solve it and I changed it to
--------
currentnode.ChildNode.Add(node);
--------

It does not throw any error but, It only adds the items of listbox1, does not adds listbox2 to nodes.
Thanks again...

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