110,538
社区成员
发帖
与我相关
我的任务
分享
//用于判断WinForm.TreeView里选中的节点是否全部展开
bool isNodeAllExplaned(TreeNode node){
if(node==null){
return false;
}
if(node.Nodes.Count>0){
//如果有子节点而且未展开,返回False
if(!node.IsExpanded){
return false;
}
else{
foreach(TreeNode subnode in node.Nodes){
if(subnode.Nodes.Count==0){
if(!subnode.IsExpanded){
return false;
}
else{
return true;
}
}
isNodeAllExplaned(subnode);
//自以为要用“迭代”
}
return false;
//这里如果不添加上面这句就报错说缺少一个返回值,但是添加上就会错
//添加上之后,只有最后一个有子节点的展开的节点返回 错误的false
//如果是return true就更错了
}
}
else{
return true;
}
}
private IEnumerable<TreeNode> GetChilds(TreeNode node)
{
var nodes = node.Nodes.Cast<TreeNode>();
return nodes.Concat(nodes.SelectMany(x => GetChilds(x)));
}
public bool isNodeAllExplaned(TreeNode node)
{
var list = GetChilds(node);
foreach (var item in list)
{
if (!item.IsExpanded)
return false;
}
return true;
}
蛋疼 先获取该节点下的所有子节点。。然后遍历。。就这么简单bool isNodeAllExplaned(TreeNode node)
{
if (!node.IsExpanded)
return false;
foreach (TreeNode x in node.Nodes)
{
if (!isNodeAllExplaned(x))
return false;
}
return true;
}
}