报错:Instance argument: cannot convert from 'System.Windows.Forms.TreeNodeCollection' to 'System.Data.EnumerableRowCollection' (CS1929);'System.Windows.Forms.TreeNodeCollection' does not contain a definition for 'Cast' and the best extension method overload 'System.Data.EnumerableRowCollectionExtensions.Cast<TResult>(System.Data.EnumerableRowCollection)' has some invalid arguments (CS1928);
TreeView 竟然木有集成判断是否全部展开的方法、、
遍历TreeNode所有节点的磁盘目录示例 private void ListViewShow(TreeNode NodeDir)//初始化ListView控件,把TrreView控件中的数据添加进来 { ListViewFile.Clear(); if (NodeDir.Parent == null)// 如果当前TreeView的父结点为空,就把我的电脑下的分区名称添加进来 { foreach (string DrvName in Directory.GetLogicalDrives())//获得硬盘分区名 { ListViewItem ItemList = new ListViewItem(DrvName); ListViewFile.Items.Add(ItemList);//添加进来 } }//codego.net/1/1/1/ else//如果当前TreeView的父结点不为空,把点击的结点,做为一个目录文件的总结点 { foreach (string DirName in Directory.GetDirectories((string)NodeDir.Tag))//编历当前分区或文件夹所有目录 { ListViewItem ItemList = new ListViewItem(DirName); ListViewFile.Items.Add(ItemList); } foreach (string FileName in Directory.GetFiles((string)NodeDir.Tag))//编历当前分区或文件夹所有目录的文件 { ListViewItem ItemList = new ListViewItem(FileName); ListViewFile.Items.Add(ItemList); } } }
{
if (!node.IsExpanded)
return false; foreach (TreeNode x in node.Nodes)
{
if (!isNodeAllExplaned(x))
return false;
} return true;
}
}
递归的时候你居然不判断返回值?
那么你需要知道它的再次调用的时候(里面判断的是子项)返回的是true还是false
......感觉有点说不下去了.既然1楼给你代码了,你就照抄好了
逻辑问题,想说明白,你得明白逻辑.你不明白,那说也白说
{
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;
}
蛋疼 先获取该节点下的所有子节点。。然后遍历。。就这么简单
1#写的代码只判断只当节点及其子孙节点是否全部展开,所以,你只需要传入TreeView.Root根节点就可以了啊
竟然木有集成判断是否全部展开的方法、、
private void ListViewShow(TreeNode NodeDir)//初始化ListView控件,把TrreView控件中的数据添加进来
{
ListViewFile.Clear();
if (NodeDir.Parent == null)// 如果当前TreeView的父结点为空,就把我的电脑下的分区名称添加进来
{
foreach (string DrvName in Directory.GetLogicalDrives())//获得硬盘分区名
{
ListViewItem ItemList = new ListViewItem(DrvName);
ListViewFile.Items.Add(ItemList);//添加进来
}
}//codego.net/1/1/1/
else//如果当前TreeView的父结点不为空,把点击的结点,做为一个目录文件的总结点
{
foreach (string DirName in Directory.GetDirectories((string)NodeDir.Tag))//编历当前分区或文件夹所有目录
{
ListViewItem ItemList = new ListViewItem(DirName);
ListViewFile.Items.Add(ItemList);
}
foreach (string FileName in Directory.GetFiles((string)NodeDir.Tag))//编历当前分区或文件夹所有目录的文件
{
ListViewItem ItemList = new ListViewItem(FileName);
ListViewFile.Items.Add(ItemList);
}
}
}
竟然木有集成判断是否全部展开的方法、、那会不会在别的类里?