1

下面的代码用于在用户控件中加载树视图的 xml 文件。不知何故,我觉得展开/关闭的代码编码不好。有没有其他方法可以以更简洁的形式对此进行编码?

try{
    this.tvTraceSelect.Progress = true;
    this.tvTraceSelect.UnitsPath = unitsPath;
    this.tvTraceSelect.TracesPath = tracesPath;
    this.tvTraceSelect.View = Traces.View.Files;
    this.tvTraceSelect.TreeView.CollapseAll();
    if (this.tvTraceSelect.TreeView.Nodes.Count > 0)
    {
        this.tvTraceSelect.TreeView.Nodes[0].Expand();
        if (this.tvTraceSelect.TreeView.Nodes[0].Nodes.Count > 0)
        {
            this.tvTraceSelect.TreeView.Nodes[0].Nodes[0].Expand();
            if (this.tvTraceSelect.TreeView.Nodes[0].Nodes[0].Nodes.Count > 0)
            {
                this.tvTraceSelect.TreeView.Nodes[0].Nodes[0].Nodes[0].Expand();
                if (this.tvTraceSelect.TreeView.Nodes[0].Nodes[0].Nodes[0].Nodes.Count > 0)
                {
                    this.tvTraceSelect.TreeView.Nodes[0].Nodes[0].Nodes[0].Nodes[0].Expand();
                }
            }
        }
    }
}catch (Exception _excep)
{
    MessageBox.Show(this, "An error occured during the initialization of the InitTracesView .\nDetails: " 
            + _excep.Message + "\n\n" + _excep.StackTrace, "Initialization error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
4

1 回答 1

1
public void ExpandFirst(TreeNodeCollection nodes)
{
   if (nodes.Count > 0)
   {
      nodes[0].Expand();
      ExpandFirst(nodes[0].Nodes);
   }
}

像这样称呼它:

ExpandFirst(this.tvTraceSelect.TreeView.Nodes)
于 2012-06-11T13:39:46.063 回答