0

Earlier Today I asked a question about getting Tree view data into a Combo Box.

    public MainForm()
    {
        InitializeComponent();

            var list = TVProperties.Nodes
                   .Cast<TreeNode>()
                   .Select(x => x.Text)
                   .ToList();

        CmboExpenseType.DataSource = list;

        var Clist = TVProperties.Nodes[0].Nodes
       .Cast<TreeNode>()
       .Select(x => x.Text)
       .ToList();

        CmboExpenseDetail.DataSource = Clist;

    }

The Above Code is what I use when starting the program. This helps me get the Parent & Child Node but when the parent node is changed the child node isn't changing.

This is the image of the two Combo Boxes I have now the top one displays the Parent Node and the Child Node.

I have tried to use Fred's answer in the SelectedIndexChange on the Combo Box but it's not working or I don't understand the way it should.

private void CmboExpenseType_SelectedIndexChanged(object sender, EventArgs e)
{
    var node = CmboExpenseType.SelectedItem as TreeNode;
    if(node == null)
        return;

    TVProperties.SelectedNode = node; 
}

This is my Tree View and some Child Nodes. I was wondering if I was doing something wrong code wise and if there was any help that you could give me.

Community
  • 1
  • 1
Logan Walker
  • 17
  • 1
  • 8

2 Answers2

0

You need to set focus on treeview:

private void CmboExpenseType_SelectedIndexChanged(object sender, EventArgs e)
{
    var node = CmboExpenseType.SelectedItem as TreeNode;
    if(node == null)
        return;

    TVProperties.Focus();
    TVProperties.SelectedNode = node; 
}
terrybozzio
  • 4,424
  • 1
  • 19
  • 25
0

c# winForm Combobox with dropdown tree view you can Use Below Code for a tree view

namespace DemoApp

{ public partial class Form2 : Form { public Form2() { InitializeComponent(); }

    public void view_dept_tree()
    {
        try
        {
            List<department> departments = getDept();     
            ComboTreeNode nod2 = new ComboTreeNode(departments[0].deptid + "-" + departments[0].deptname.ToString());
            ctbImages.Nodes.Add(nod2);
            PopulateTreeView2(Convert.ToInt32(departments[0].deptid), nod2);
            ctbImages.Sort();
            ctbImages.SelectedNode = ctbImages.Nodes[0];

        }
        catch (Exception)
        { }
    }    
    private void PopulateTreeView2(int parentId, ComboTreeNode parentNode)
    {
        try
        {
            var departments = getDept().Where(x=>x.subdeptid== parentId).ToList();
            ComboTreeNode childNode;
            for (int i = 0; i < departments.Count; i++)
            {
                if (parentNode == null)
                    childNode = ctbImages.Nodes.Add(departments[i].deptid.ToString() + "-" + departments[i].deptname.ToString());
                else
                    childNode = parentNode.Nodes.Add(departments[i].deptid.ToString() + "-" + departments[i].deptname.ToString());

                PopulateTreeView2(Convert.ToInt32(departments[i].deptid.ToString()), childNode);

            }
        }
        catch (Exception)
        { }
    }
    public List<department> getDept()
    {
        List<department> departments = new List<department>();
        departments.Add(new department { deptid = 1, deptname = "main", subdeptid = 0 });
        departments.Add(new department { deptid = 2, deptname = "d1", subdeptid = 1 });
        departments.Add(new department { deptid = 3, deptname = "d2", subdeptid = 1 });
        departments.Add(new department { deptid = 4, deptname = "d3", subdeptid = 1 });
        departments.Add(new department { deptid = 5, deptname = "d4", subdeptid = 2 });
        departments.Add(new department { deptid = 6, deptname = "d5", subdeptid = 2 });
        departments.Add(new department { deptid = 7, deptname = "d6", subdeptid = 2 });
        departments.Add(new department { deptid = 8, deptname = "d7", subdeptid = 1 });
        departments.Add(new department { deptid = 9, deptname = "d8", subdeptid = 1 });
        departments.Add(new department { deptid = 10, deptname = "d9", subdeptid = 5 });
        departments.Add(new department { deptid = 11, deptname = "d10", subdeptid = 5 });
        departments.Add(new department { deptid = 12, deptname = "d11", subdeptid = 5 });
        departments.Add(new department { deptid = 13, deptname = "d12", subdeptid = 5 });
        departments.Add(new department { deptid = 14, deptname = "d13", subdeptid = 13 });
        departments.Add(new department { deptid = 15, deptname = "d14", subdeptid = 13 });
        departments.Add(new department { deptid = 16, deptname = "d15", subdeptid = 13 });
        departments.Add(new department { deptid = 17, deptname = "demo13", subdeptid = 14 });
        departments.Add(new department { deptid = 18, deptname = "demo14", subdeptid = 14 });
        departments.Add(new department { deptid = 19, deptname = "demo15", subdeptid = 14 });
        return departments;
    }
    private void Form2_Load(object sender, EventArgs e)
    {
        view_dept_tree();
        ctbImages.ExpandAll();
    }

    public class department
    {
        public int deptid { get; set; }
        public string deptname { get; set; }

        public int subdeptid { get; set; }
    }
   
    private void button1_Click_1(object sender, EventArgs e)
    {
        for (int i = 0; i < ctbImages.CheckedNodes.ToList().Count; i++)
        {
            textBox1.Text = ctbImages.CheckedNodes.ToList()[i].Text;
            MessageBox.Show(ctbImages.CheckedNodes.ToList()[i].Text);
        }
    }
}

}