日期:2014-05-18  浏览次数:20957 次

关于winform中Treeview的2个问题,附上代码,请大家帮忙看看,在线等待大家的回复!!!谢谢
一个生成tree 的方法 , 我想让它初始根据条件选中某个checkbox 和全部展开所有的目录 ,
看来很常见的问题, 但是我总没有看到效果, 不知道我哪里写错了 , 请大家帮忙看看. 谢谢 !
虽然是很常见的问题,但我在网上找了许久,还是运行起来还是没看到我要的效果, 
因此没办法, 厚着脸皮上来了 .. 

我的生成tree 的代码如下 ,checkbox 在 InitializeComponent()里设置为true 了, 
this.treeView1.CheckBoxes = true;  
生成tree 的方法在 InitializeComponent() 后 .

C# code
private void getTree(string outboundno, string pn, string custpo)
        {   
            __WMS_Tree[] trees = server.pickingTree_Search(outboundno, pn, custpo);
            if(trees!=null) 
            {
                treeNodeCtn = new TreeNode[trees.Length];
                for (int i=0; i<trees.Length; i++)
                {
                    //add ctn nodes
                    __WMS_Tree t = trees[i];
                    string ctnNo = t.cartonNo;
                   
                    string flag = t.carton_flag==null?"1":t.carton_flag;  
                    treeNodeCtn[i] = new TreeNode(ctnNo);
                    treeNodeCtn[i].Expand();  //这里展开不了下面的节点
                    if (Convert.ToInt32(flag)>=3)
                    {
                        Console.WriteLine("carton"+i+":"+flag+"$$$");
                           treeNodeCtn[i].Checked = true;     // 初始设置为选中状态没起作用 
                    }
                    
                    //add box nodes
                    __WMS_Box[] boxs = trees[i].boxSet;
                  
                    if(boxs.Length>0) 
                    {
                        treeNodeBox = new TreeNode[boxs.Length];
                        for (int j=0; j<boxs.Length; j++)
                        {
                            __WMS_Box b = boxs[j];
                            string boxNo = b.boxNo;
                            string box_flag = b.flag==null?"1":b.flag;
                            treeNodeBox[j] = new TreeNode(boxNo);
                        //    treeNodeBox[j].Expand = true ;
                            if (Convert.ToInt32(box_flag)>=3)
                            {
                                Console.WriteLine("box"+j+":"+flag+"######");
                                treeNodeBox[j].Checked = true ;
                            }
                            
                            treeNodeCtn[i].Nodes.Add(treeNodeBox[j]);
                        }
                    }
                    treeView1.Nodes.Add(treeNodeCtn[i]);

                    //add event handler
                    treeView1.AfterSelect += new TreeViewEventHandler(treeView1_AfterSelect);
                }
            }
        }



请注意看我注释的部分 // 那里, 先谢谢大家了!!!

------解决方案--------------------
我先帮你顶着,好帖不让沉下去
------解决方案--------------------
帮LZ顶下``````
------解决方案--------------------
所有节点展开应该用ExpandAll()这个方法
C# code
this.treeView1.ExpandAll();

------解决方案--------------------
good
------解决方案--------------------
treeNodeCtn[i].Checked = true;
是没错的
但是没有checkbox是不行的
而且,你的写法很怪异
------解决方案--------------------
treeView1.Nodes[i].Expand(); i 是符合你要求的 ,比如选中的节点
treeView1.Nodes[i].Checked = true; 打勾 。前提是 treeView1.CheckBoxes = true;
------解决方案--------------------
楼主的写法很奇怪(一般人都是一个node一个node实例化,像楼主这样一次实例化一个node数组再对数组中的node逐个实例化的写法实在少见),不过我用楼主的代码做了个测试,是没问题的。