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);
}
}
}
}