我有以下代码适用于具有x-children子项的一个父treenode,但我也想创建另一个父节点但是此时使用dwg文件名填充它...由于某种原因它仅在采取后切断使用xls扩展的帐户我还想为其他类型的扩展添加具有子名称的父级。按照正确工作的代码我添加了incorect woring代码?
工作代码:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim di As New IO.DirectoryInfo("c:\la")
Dim diar1 As IO.FileInfo() = di.GetFiles()
Dim dra As IO.FileInfo
For Each dra In diar1
'Dim sr As StreamReader = New StreamReader(dra.FullName)
If System.IO.Path.GetExtension(dra.Name).ToLower() = ".xls" Then
If TreeView1.Nodes.Count = 0 Then TreeView1.Nodes.Add("Offerts")
TreeView1.Nodes(0).Nodes.Add(dra.Name)
End If
Next
End Sub
现在编程错误的扩展代码:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim di As New IO.DirectoryInfo("c:\la")
Dim diar1 As IO.FileInfo() = di.GetFiles()
Dim dra As IO.FileInfo
For Each dra In diar1
'Dim sr As StreamReader = New StreamReader(dra.FullName)
If System.IO.Path.GetExtension(dra.Name).ToLower() = ".xls" Then
If TreeView1.Nodes.Count = 0 Then TreeView1.Nodes.Add("Offerts")
TreeView1.Nodes(0).Nodes.Add(dra.Name)
End If
If System.IO.Path.GetExtension(dra.Name).ToLower() = ".dwg " Then
If TreeView1.Nodes.Count = 3 Then TreeView1.Nodes.Add("Technical data")
TreeView1.Nodes(1).Nodes.Add(dra.Name)
End If
Next
End Sub
答案 0 :(得分:0)
当它到达这个If语句时:
If TreeView1.Nodes.Count = 0 Then TreeView1.Nodes.Add("Technical data")
它没有添加"技术数据"节点,因为TreeView1.Nodes.Count不是零(它是一个),因为它已经创建了" Offes"节点和Offerts下的一个或多个节点。
要做到这一点正中途(要做到这一点"真的"对可能不太好进入这里),你需要在创建根节点时添加一个键,以便你可以使用查找函数是否已经创建,以及将文件名放在正确的节点下。因此,您只需将密钥添加为第二个参数TreeView1.Nodes.Add("Offerts")
,而不仅仅是TreeView1.Nodes.Add("Offerts", "Offerts")
。然后,您可以使用TreeView的Find
函数来枚举该节点以检查它是否存在,添加子节点,删除它等等。
Dim di As New IO.DirectoryInfo("c:\la")
Dim diar1 As IO.FileInfo() = di.GetFiles()
Dim dra As IO.FileInfo
For Each dra In diar1
If System.IO.Path.GetExtension(dra.Name).ToLower() = ".xls" Then
If TreeView1.Nodes.Find("Offerts", False).Length = 0 Then TreeView1.Nodes.Add("Offerts", "Offerts")
TreeView1.Nodes.Find("Offerts", False)(0).Nodes.Add(dra.Name)
End If
If System.IO.Path.GetExtension(dra.Name).ToLower() = ".dwg" Then
If TreeView1.Nodes.Find("Technical data", False).Length = 0 Then TreeView1.Nodes.Add("Technical data", "Technical data")
TreeView1.Nodes.Find("Technical Data", False)(0).Nodes.Add(dra.Name)
End If
Next