tv是TREEVIEW,STRFN是文件名dim strFN as string dim Fnum as single dim strTMP as string dim nd as node strfn=***********(file name at any location) fnum=freefile() open strfn for input as #fnum do until eof(fnum) line input #fnum,strtmp set nd=tv.nodes.add(,,tvwchild,,strtmp) loop close #fnum
dim s as string open "a.txt" for input as #1 do line input #1,s tree1.add .... loop while not eof(1) close #1
Option ExplicitPrivate Function ReadFileLine(cfile As String, cLine As Integer) As String Dim s As String Dim fsoTest As New FileSystemObject, file1 As File, ts As TextStream Set file1 = fsoTest.GetFile(cfile) Set ts = file1.OpenAsTextStream(ForReading) s = ts.ReadAll Dim Str() As String Str = Split(s, vbCrLf) ReadFileLine = Str(cLine - 1) Set ts = Nothing End FunctionPrivate Function FileLineCountA(cfile As String) As Integer Dim s As String Dim fsoTest As New FileSystemObject, file1 As File, ts As TextStream Set file1 = fsoTest.GetFile(cfile) Set ts = file1.OpenAsTextStream(ForReading) s = ts.ReadAll Dim Str() As String Str = Split(s, vbCrLf) FileLineCountA = UBound(Str) + 1 Set ts = Nothing End FunctionPrivate Sub Form_Load() Dim LineCount As Integer, i As Integer, FileName As String FileName = App.Path + "\123.txt" LineCount = FileLineCountA(FileName) For i = 1 To LineCount Me.TreeView1.Nodes.Add , , , ReadFileLine(FileName, i) Next End Sub
对了,要引用microsoft scripting runtime.
当然,还可以简化: Option Explicit Private Sub Form_Load() Dim FileName As String FileName = App.Path + "\123.txt" addnode FileName, TreeView1 End Sub Private Sub addnode(cfile As String, mytreeview As treeview) Dim s As String, i As Integer Dim fsoTest As New FileSystemObject, file1 As File, ts As TextStream Set file1 = fsoTest.GetFile(cfile) Set ts = file1.OpenAsTextStream(ForReading) s = ts.ReadAll Dim Str() As String Str = Split(s, vbCrLf) For i = 0 To UBound(Str) mytreeview.Nodes.Add , , , Str(i) Next Set ts = Nothing End Sub
dim Fnum as single
dim strTMP as string
dim nd as node
strfn=***********(file name at any location)
fnum=freefile()
open strfn for input as #fnum
do until eof(fnum)
line input #fnum,strtmp
set nd=tv.nodes.add(,,tvwchild,,strtmp)
loop
close #fnum
open "a.txt" for input as #1
do
line input #1,s
tree1.add ....
loop while not eof(1)
close #1
Dim s As String
Dim fsoTest As New FileSystemObject, file1 As File, ts As TextStream
Set file1 = fsoTest.GetFile(cfile)
Set ts = file1.OpenAsTextStream(ForReading)
s = ts.ReadAll
Dim Str() As String
Str = Split(s, vbCrLf)
ReadFileLine = Str(cLine - 1)
Set ts = Nothing
End FunctionPrivate Function FileLineCountA(cfile As String) As Integer
Dim s As String
Dim fsoTest As New FileSystemObject, file1 As File, ts As TextStream
Set file1 = fsoTest.GetFile(cfile)
Set ts = file1.OpenAsTextStream(ForReading)
s = ts.ReadAll
Dim Str() As String
Str = Split(s, vbCrLf)
FileLineCountA = UBound(Str) + 1
Set ts = Nothing
End FunctionPrivate Sub Form_Load()
Dim LineCount As Integer, i As Integer, FileName As String
FileName = App.Path + "\123.txt"
LineCount = FileLineCountA(FileName)
For i = 1 To LineCount
Me.TreeView1.Nodes.Add , , , ReadFileLine(FileName, i)
Next
End Sub
Option Explicit
Private Sub Form_Load()
Dim FileName As String
FileName = App.Path + "\123.txt"
addnode FileName, TreeView1
End Sub
Private Sub addnode(cfile As String, mytreeview As treeview)
Dim s As String, i As Integer
Dim fsoTest As New FileSystemObject, file1 As File, ts As TextStream
Set file1 = fsoTest.GetFile(cfile)
Set ts = file1.OpenAsTextStream(ForReading)
s = ts.ReadAll
Dim Str() As String
Str = Split(s, vbCrLf)
For i = 0 To UBound(Str)
mytreeview.Nodes.Add , , , Str(i)
Next
Set ts = Nothing
End Sub