一个生成tree 的方法 , 我想让它初始根据条件选中某个checkbox 和全部展开所有的目录 ,
看来很常见的问题, 但是我总没有看到效果, 不知道我哪里写错了 , 请大家帮忙看看. 谢谢 !
虽然是很常见的问题,但我在网上找了许久,还是运行起来还是没看到我要的效果,
因此没办法, 厚着脸皮上来了 .. 我的生成tree 的代码如下 ,checkbox 在 InitializeComponent()里设置为true 了,
this.treeView1.CheckBoxes = true;
生成tree 的方法在 InitializeComponent() 后 .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);
}
}
}
请注意看我注释的部分 // 那里, 先谢谢大家了!!!
看来很常见的问题, 但是我总没有看到效果, 不知道我哪里写错了 , 请大家帮忙看看. 谢谢 !
虽然是很常见的问题,但我在网上找了许久,还是运行起来还是没看到我要的效果,
因此没办法, 厚着脸皮上来了 .. 我的生成tree 的代码如下 ,checkbox 在 InitializeComponent()里设置为true 了,
this.treeView1.CheckBoxes = true;
生成tree 的方法在 InitializeComponent() 后 .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);
}
}
}
请注意看我注释的部分 // 那里, 先谢谢大家了!!!
this.treeView1.ExpandAll();
不知道 单个的treeNodeCtn[i].Expand();
这样写 为什么不行 ? 另外就是 这样写来让一个checkbox勾选 为什么也没起作用呢?
treeNodeCtn[i].Checked = true; 哪位给指点一下 ? 谢谢了!
是没错的
但是没有checkbox是不行的
而且,你的写法很怪异
treeView1.Nodes[i].Checked = true; 打勾 。前提是 treeView1.CheckBoxes = true;
但 这是 在所有 树 节点 都生成 之后 ..而我实际上的条件都是 在生成 树的过程中(所有节点并未完成生成) 来根据条件 决定 该节点 是否 展开 和 勾选.也就是这个时候 treeView1.Nodes[i] 的 nodes 还未 添加上, 即程序 还没跑到 treeView1.Nodes.Add(treeNodeCtn[i]) 这个地方 ..我这样是不是有点怪了, 难道要做2个循环吗? 即先生成所有节点, 再判断某些节点是否 展开 和 勾选 ?而不能在 完全 生成 树 之前 一边判断呢 ??
{
TreeNode[] nodes = new TreeNode[3];
nodes[0] = new TreeNode("AAA");
nodes[0].Expand();
nodes[0].Checked = true;
treeView1.Nodes.Add(nodes[0]);
}