*******请教,关于Treeview控件*******
请问如何将一个文本(有固定格式)加入Treeview控件
文本格式如下:
第一章 ****
第一节 *** (前面使用Tab字符来增加缩进)
第二节 ***
第二章 ****
第一节 ***
....
....
问题点数:80、回复次数:2Top
1 楼DemonXHunter(恶魔猎手)回复于 2003-12-03 10:23:05 得分 40
我不知道你这些文本存于何处,所以我暂时定义了一个数组来存发了几行文本,这是个例子,但是你稍加修改就可以使用了,主要是修改循环。
Private Sub Command1_Click()
Dim lines(6) As String
Dim tabCount As Integer
Dim showText As String
Dim levelNode(8) As Node
lines(0) = "第一章 ****"
lines(1) = vbTab & "第1节 ***"
lines(2) = vbTab & "第2节 ***"
lines(3) = "第二章 ***"
lines(4) = vbTab & "第1节 ***"
Tree1.Nodes.Clear
For i = 0 To 4
tabCount = getTabCount(lines(i))
showText = LTrim(lines(i))
If tabCount = 0 Then
Set levelNode(0) = Tree1.Nodes.Add(, tvwChild, , showText)
ElseIf Not levelNode(tabCount - 1) Is Nothing Then
Set levelNode(tabCount) = Tree1.Nodes.Add(levelNode(tabCount - 1), tvwChild, , showText)
Else
Debug.Print "文本格式有错误!" & lines(i)
End If
Next i
End Sub
Private Function getTabCount(str As String) As Integer
For i = 1 To Len(str)
If Mid(str, i, 1) <> vbTab Then
getTabCount = i - 1
Exit Function
End If
Next i
getTabCount = Len(str)
End FunctionTop
2 楼SoHo_Andy(冰)回复于 2003-12-03 10:32:44 得分 40
同意,修改为读取文本文件内容
已经完全满足你的要求
Private Sub Command1_Click()
Dim tabCount As Integer
Dim showText As String
Dim levelNode(8) As Node
Dim FileContents As String
Dim fileNum As Integer
Dim LineInfo() As String
fileNum = FreeFile
Open "d:\aa.txt" For Binary As #fileNum
FileContents = Space(LOF(fileNum))
Get #fileNum, , FileContents
Close fileNum
LineInfo = Split(FileContents, vbCrLf)
Tree1.Nodes.Clear
For i = 0 To 4
tabCount = getTabCount(LineInfo(i))
showText = LTrim(LineInfo(i))
If tabCount = 0 Then
Set levelNode(0) = Tree1.Nodes.Add(, tvwChild, , showText)
ElseIf Not levelNode(tabCount - 1) Is Nothing Then
Set levelNode(tabCount) = Tree1.Nodes.Add(levelNode(tabCount - 1), tvwChild, , showText)
Else
Debug.Print "文本格式有错误!" & LineInfo(i)
End If
Next i
End Sub
Private Function getTabCount(str As String) As Integer
For i = 1 To Len(str)
If Mid(str, i, 1) <> vbTab Then
getTabCount = i - 1
Exit Function
End If
Next i
getTabCount = Len(str)
End Function
结贴Top




