以下代码适用于在用户控件中加载树视图的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);
}
答案 0 :(得分:1)
public void ExpandFirst(TreeNodeCollection nodes)
{
if (nodes.Count > 0)
{
nodes[0].Expand();
ExpandFirst(nodes[0].Nodes);
}
}
这样称呼:
ExpandFirst(this.tvTraceSelect.TreeView.Nodes)