日期:2014-05-18 浏览次数:21109 次
private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) { this.tabControl1.TabPages.Clear(); if (e.Node.Level == 0) { foreach (TreeNode node in this.treeView1.Nodes[0].Nodes)//this.treeView1.Nodes[0]是销售基础数据所在的节点,具体你再调整 this.tabControl1.TabPages.Add(node.Text, node.Text); return; } this.tabControl1.TabPages.Add(e.Node.Text, e.Node.Text, 0); }
------解决方案--------------------
TreeNode currNode;//当前节点 List<TabPage> list;//保存所有的TabPages private void Form1_Load(object sender, EventArgs e) { //把所有的TabPages放在list中 list = new List<TabPage>(); foreach (TabPage page in this.tabControl1.TabPages) list.Add(page); } private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) { currNode = e.Node; this.tabControl1.TabPages.Clear(); if (e.Node.Level == 0)//单击父节点,显示全部 { this.tabControl1.TabPages.AddRange(list.ToArray()); return; } //在list中找到对应的TabPage加到TabControl中 TabPage p = list.Find(new Predicate<TabPage>(find)); if(p != null) this.tabControl1.TabPages.Add(p); } bool find(TabPage page) { return page.Text == currNode.Text; }
------解决方案--------------------
当然,list.Find()用委托实现的话更替紧凑写
TreeNode currNode; List<TabPage> list; private void Form1_Load(object sender, EventArgs e) { list = new List<TabPage>(); foreach (TabPage page in this.tabControl1.TabPages) list.Add(page); } private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) { currNode = e.Node; this.tabControl1.TabPages.Clear(); if (e.Node.Level == 0) { this.tabControl1.TabPages.AddRange(list.ToArray()); return; } TabPage p = list.Find(new Predicate<TabPage>(delegate(TabPage pageTem) { return pageTem.Text == currNode.Text; })); if(p != null) this.tabControl1.TabPages.Add(p); }