【问题标题】:how to make change in all TreeView's that exist in a form如何更改表单中存在的所有 TreeView
【发布时间】:2011-11-10 14:46:37
【问题描述】:

我的 winform 应用程序中有一些 TreeView。我正试图一口气对所有这些进行大规模更改。所以我写了下面的代码:

private void ResetTreeViewColors()
{
    foreach (TreeView tv in this.Controls)
    {
        foreach(TreeNode tn in tv.Nodes)
        {
            tn.BackColor = Color.White;
            tn.ForeColor = Color.Black;
        }
    }
}

它可以编译,但是当我调用此方法时,我收到一条错误消息:

无法将“System.Windows.Forms.Button”类型的对象转换为“System.Windows.Forms.TreeView”类型。

很明显,我还有一些其他控件,例如按钮等...我想知道我的代码有什么问题可以编译但运行失败!

【问题讨论】:

    标签: c# winforms treeview controls


    【解决方案1】:

    this.Controls 集合包含所有控件,而不仅仅是树视图,我很惊讶它没有出现编译错误甚至警告。 在尝试将其用作 TreeView 之前,您需要检查每个控件的类型:

    private void ResetTreeViewColors() { 
        foreach (Control tvc in this.Controls) { 
            if (tvc is TreeView) {
                TreeView tv = (TreeView)tvc;
                foreach(TreeNode tn in tv.Nodes) { 
                    tn.BackColor = Color.White; 
                    tn.ForeColor = Color.Black; 
                }
            } 
        } 
    } 
    

    【讨论】:

    • 谢谢,但您的代码无法编译。它给了Error 1 Cannot implicitly convert type 'System.Windows.Forms.Control' to 'System.Windows.Forms.TreeView'. An explicit conversion exists (are you missing a cast?)
    • 错误在这里TreeView tv = tvc; tvc 有问题:P
    • 对不起,正如错误所说,它缺少演员表。我现在已经添加了。
    【解决方案2】:

    您的表单中还有一些不是 TreeView 类型的控件,因此不能强制转换为一个。试试这个:

    foreach (control c in this.Controls)
    {
      TreeView tr = c as TreeView;
      if(tr != null)
        // your logic here
    }
    

    【讨论】:

    • 这与 OP 存在完全相同的问题,因为它在尝试使用它之前实际上并没有检查控件是否为 TreeView。
    • as TreeView 结合 tr != null 执行检查。
    • 我这样做了,但由于某种原因它什么也没做! ` foreach (Control c in this.Controls) { TreeView tr = c as TreeView; if (tr != null) { foreach (tr.Nodes 中的 TreeNode tn) { tn.BackColor = Color.White; tn.ForeColor = 颜色.黑色; } } }`
    • 除了“它不做任何事情”之外,您还需要提供更多信息。例如,当您在调试器中检查每个 c 时,它们是什么?
    • 抱歉,我不知道 C# > 2.0 上的这种行为。如果答案被编辑,我将删除我的反对票。
    【解决方案3】:

    您应该注意,它可以是表单内的嵌套控件和 treeView 根节点内的嵌套节点。所以最终的代码应该是这样的:

    ResetChildTreeViews(this); // reset all treeviews within a form
    //...
    static void ResetChildTreeViews(Control container) {
        foreach(Control ctrl in container.Controls) {
            if(ctrl is TreeView)
                ResetTreeViewColors(ctrl as TreeView);
            else ResetChildTreeViews(ctrl);
        }
    }
    static void ResetTreeViewColors(TreeView treeView) {
        foreach(TreeNode node in treeView.Nodes)
            ResetTreeNodeColors(node);
    }
    static void ResetTreeNodeColors(TreeNode node) {
        node.BackColor = Color.White;
        node.ForeColor = Color.Black;
        foreach(TreeNode childNode in node.Nodes) 
            ResetTreeNodeColors(childNode);
    }
    

    【讨论】:

      猜你喜欢
      • 2014-12-31
      • 2022-12-29
      • 2013-04-26
      • 1970-01-01
      • 1970-01-01
      • 2017-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多