1

我最近一直在玩treenodes,我希望能够在创建特定节点后从特定节点中删除一个复选框,具体取决于我的控件是1还是0。我完成了大部分代码,只是这部分我不太确定如何实现。

private void ListDirectory(TreeView treeView, string path) 
        {     
            treeView.Nodes.Clear();   
            var rootDirectoryInfo = new DirectoryInfo(path);    
            treeView.Nodes.Add(CreateDirectoryNode(rootDirectoryInfo)); 
        }     
    private static TreeNode CreateDirectoryNode(DirectoryInfo directoryInfo) 
    {



        var directoryNode = new TreeNode(directoryInfo.Name);

        foreach (var directory in directoryInfo.GetDirectories())
            directoryNode.Nodes.Add(CreateDirectoryNode(directory));

        foreach (var file in directoryInfo.GetFiles())
        {
            int check =0;                  
            try
            {
                string s = "";
                s = directoryInfo.FullName + "\\" + file.Name;
                List<string> row, row2 = new List<string>();

                using (StreamReader readFile = new StreamReader(s))
                {
                    row = (readFile.ReadLine().Split(',').ToList());
                    try 
                    {
                        row2 = (readFile.ReadLine().Split(',').ToList());
                        check = 1;
                    }
                    catch { }
                }

                TreeNode[] headerNodes = new TreeNode[row.Count];

                for (int i = 0; i < row.Count; i++)
                {
                    if (check == 0)
                    {
                        //IN HERE SOMEWHERE//
                        headerNodes[i].BackColor = Color.Red;
                        headerNodes[i].ForeColor = Color.White;
                    }
                    headerNodes[i] = new TreeNode(row[i]);

                }
                directoryNode.Nodes.Add(new TreeNode(file.Name, headerNodes));
            }
            catch 
            {
                directoryNode.Nodes.Add(new TreeNode(file.Name));
            }
        }         
        return directoryNode; 
    } 

我确信必须有一些简单的方法来从单个节点中删除一个复选框,而不是一次删除所有的复选框,我似乎无法在任何地方找到它......

4

1 回答 1

1

有一段时间没有这样做了,但以前从来没有可能删除单个复选框。一种解决方法是关闭复选框并使用图形而不是使用单击事件在选中和未选中之间切换。甚至可以扩展到更复杂的状态。

于 2012-05-17T17:50:24.497 回答