请问如何将txt的内容导入到list框
还有如何将list的内容保存为txt文件。

解决方案 »

  1.   

    '读取
        Dim ReadStr As String
        Open "c:\a.txt" For Input As #1
        Do While Not EOF(1)
            Line Input #1, ReadStr
            List1.AddItem ReadStr
        Loop
        Close #1
        
        '保存
        Dim i As Integer
        Open "c:\b.txt" For Output As #1
        For i = 0 To List1.ListCount
            Print #1, List1.List(i)
        Next
        Close #1
      

  2.   


    '增加两个按钮和一个列表框
    Option Explicit
    Private Sub SaveList(ListBox1 As ListBox, ByVal FileName As String)
        Dim fnum As Integer
        Dim i As Long
        With ListBox1
            If .ListCount = 0 Then
                Exit Sub
            End If
            If FileName = "" Then
                Exit Sub
            End If
            fnum = FreeFile
            Open FileName For Output As #fnum
            For i = 0 To .ListCount - 1
                Print #fnum, .List(i)
            Next i
            Close #fnum
        End With
    End SubPrivate Sub GetList(ListBox1 As ListBox, ByVal FileName As String)
        Dim fnum As Integer
        Dim i As Long
        Dim s As String
        
        With ListBox1
            If .ListCount = 0 Then
                Exit Sub
            End If
            If FileName = "" Then
                Exit Sub
            End If
            fnum = FreeFile
            Open FileName For Input As #fnum
            Do While Not EOF(fnum)
                Line Input #fnum, s
                .AddItem s
            Loop
            Close #fnum
        End With
    End SubPrivate Sub Command1_Click()
        SaveList List1, "C:\list.txt" '保存
    End SubPrivate Sub Command2_Click()
        GetList List1, "C:\list.txt" '读取
    End SubPrivate Sub Form_Load()
        List1.AddItem "A"
        List1.AddItem "A"
        List1.AddItem "A"
        List1.AddItem "A"
        List1.AddItem "A"
    End Sub
      

  3.   

    读取
        Dim ReadStr As String
        Open "c:\a.txt" For Input As #1
        Do While Not EOF(1)
            Line Input #1, ReadStr
            List1.AddItem ReadStr
        Loop
        Close #1
        
        '保存
        Dim i As Integer
        Open "c:\b.txt" For Output As #1
        For i = 0 To List1.ListCount
            Print #1, List1.List(i)
        Next
        Close #1
    同意,呵呵