treeview和listview的用法
今天无聊中弄了个小东西,熟悉一下对listview和treeview的用法。代码如下:
public partial class Form1 : Form { private School school; public Form1() { InitializeComponent(); Office o1 = new Office("计算机基础","王军","杨晓勇","何平","姜涛"); Office o2 = new Office("软件工程", "马建国", "陈军", "刘晓燕"); Office o3 = new Office("信息安全", "冯尧", "李建军", "张涛"); Department d1=new Department("计算机",o1,o2,o3); Office o4 = new Office("自动控制", "吴自力", "陈锋", "薛小龙"); Office o5 = new Office("工业设计", "吴淑华", "方坤", "何丽", "蔡聪"); Department d2=new Department("机电工程",o4,o5); Office o6 = new Office("信息管理", "赵民", "盛晓楠", "徐小平"); Office o7 = new Office("工商管理", "张敏", "李玲", "吕倩", "高健"); Department d3=new Department("经济管理",o6,o7); school = new School("科大", d1, d2, d3); TreeNode root = treeView1.Nodes.Add(school.ToString()); foreach(Department d in school.departments ) { TreeNode node = root.Nodes.Add(d.ToString()); foreach (Office o in d.offices) { node.Nodes.Add(o.ToString()); } } listView1.Columns.Add("姓名"); listView1.Columns.Add("电话"); listView1.Columns.Add("电子邮件"); } private void treeView1_AfterSelect(object sender, TreeViewEventArgs e) { listView1.Items.Clear(); if (e.Node.Level == 0) { foreach (Department d in school.departments) { listView1.Items.Add(d.ToString(),0); } } else if (e.Node.Level == 1) { foreach (Office o in school.departments[e.Node.Index].offices) { listView1.Items.Add(o.ToString(), 1); } } else if (e.Node.Level == 2) { foreach (string s in school.departments[e.Node.Parent.Index].offices[e.Node.Index].teachers) { listView1.Items.Add(s.ToString(), 2); } } } } public class School { public School(string name, params Department[] departments) { this.Name = name; this.departments = new List<Department>(departments); } public string Name { get; set; } public List<Department> departments { get; set; } public override string ToString() { return Name; } } public class Department { public string Name { get; set; } public List<Office> offices { get; set; } public Department(string name, params Office[] offices) { this.Name = name;
this.offices = new List<Office>(offices); } public override string ToString() { return Name + "系"; } } public class Office { public string Name { get; set; } public List<string> teachers { get; set; } public Office(string name, params string[] teachers) { this.Name = name; this.teachers = new List<string>(teachers); } public override string ToString() { return Name + "教研室"; } } 运行后截图:
浮躁的人容易问:我到底该学什么;----别问,学就对了; 浮躁的人容易问:JS有钱途吗;----建议你去抢银行; 浮躁的人容易说:我要中文版!我英文不行!----不行?学呀! 浮躁的人分两种:只观望而不学的人;只学而不坚持的人; 浮躁的人永远不是一个高手。