匿名使用者
匿名使用者 發問時間: 電腦與網際網路程式設計 · 2 年前

vb6 list 存檔問題?

各位大大好:

我利用下面的程式碼把txt的檔案讀檔到list1.list,請問如何又把list1.list另存到另一個 .txt檔呢?(p.s.檔案數超過32767)

Private Sub Command1_Click()

Dim f As Long, s() As String, i As Single, j As Single

List1.Clear

strMsg = ""

Close #f

f = FreeFile

List1.Visible = False: DoEvents

On Error GoTo ErrHandler

f = FreeFile

With CmDialog1

.CancelError = True

.DialogTitle = "開啟檔案"

.Filter = "txt File (*.*)|*.txt;|txt files(*.txt)|*.txt"

.ShowOpen

f = FreeFile

Open .FileName For Binary As #f

End With

ReDim a(LOF(f) - 1) As Byte

Get #f, , a

Close #f

s = Split(StrConv(a, vbUnicode), vbCrLf)

i = UBound(s)

If i > 32767 Then

ProgressBar1.Max = 32767: i = 32767 / i

Else

ProgressBar1.Max = i: i = 1

End If

ProgressBar1 = 0

List1.Visible = False

DoEvents

On Error Resume Next

For f = 0 To UBound(s)

List1.AddItem s(f)

j = j + 1 * i

ProgressBar1 = j

Next

List1.Visible = True

Close #f

List1.Visible = True

List2(lstCnt).Visible = True

ErrHandler:

If Err <> CancelError Then MsgBox Error

Exit Sub

End Sub

2 個解答

評分
  • John
    Lv 7
    2 年前
    最佳解答

    Private Sub btnSave_Click()

    On Error GoTo ErrorHandler

    Dim i As Long

    Dim iOutputFile As Integer

    iOutputFile = FreeFile

    Open "c:\output.txt" For Output As #iOutputFile

    For i = 0 To List1.ListItems.Count - 1

    DoEvents

    Print #iOutputFile, List1.ListItem(i).Text

    Next

    ErrorHandlerExit:

    Close #iOutputFile

    Exit Sub

    ErrorHandler:

    MsgBox err.Description, vbCritical, err.Number

    Resume ErrorHandlerExit

    End Sub

    • John
      Lv 7
      2 年前檢舉

      For i = 0 To List1.ListCount - 1
      如果這個方法唔得,妳可能要改寫ListBox為分類用多層顯示出來。其實三萬items個真係太多。

      Ref: https://msdn.microsoft.com/en-us/library/aa235161(v=vs.60).aspx

  • 2 年前

    目前用這方法可以存超過32767,但還差了一些資料沒存完整,請問怎麼修程式碼呢?

    Private Sub Command2_Click()

    Dim i As Long

    Close #f

    nCount = SendMessage(List1.hwnd, LB_GETCOUNT, 0, 0)

    With CmDialog1

    .CancelError = True

    .DialogTitle = "開啟檔案"

    .Filter = "txt File (*.*)|*.txt;|txt files(*.txt)|*.txt"

    .ShowOpen

    Text1.Text = .FileTitle

    f = FreeFile

    Open .FileName For Output As #f

    End With

    Dim s As String

    Dim strItem As String * 256

    nCount = SendMessage(List1.hwnd, LB_GETCOUNT, 0, 0)

    For i = 0 To nCount - 1

    Call SendMessageByString(List1.hwnd, LB_GETTEXT, i, strItem)

    s = Trim0(strItem)

    Print #f, s

    Next

    End Sub

還有問題?馬上發問,尋求解答。