PDA

View Full Version : [vb6] Listboxes and File Handling



Zachafer
03-31-2012, 11:08 AM
That "Browse..." dialog you see in many programs is the Microsoft Common Dialog Control. To add this into your project, go to Project -> Components (CTRL+T) and scroll down to Microsoft Common Dialog Control.

You can also create a Common Dialog Control without adding the component into your project, however this seems to fail on about half of users' computers.

This code can load a listbox from a file (line by line), or save a listbox to a file (line by line).

I have written both ways and put them into two separate modules.

With component (highly recommended):

Option Explicit

Public Sub SaveListCD(cd As CommonDialog, List As ListBox, Optional Title As String = "Save List", Optional clear As Boolean = False)
On Error GoTo Handle:

cd.Filter = "Text Files (.txt)|*.txt"
cd.FileName = vbNullString
cd.ShowSave
cd.DialogTitle = Title

If LenB(cd.FileName) = 0 Then
Exit Sub
End If
SaveFile List, cd.FileName
Exit Sub
Handle:
Debug.Print "Err #" & Err.Number & " on line " & Erl & vbNewLine & Err.Description
End Sub

Public Function LoadListCD(cd As CommonDialog, List As ListBox, Optional Title As String = "Load List", Optional clear As Boolean = True) As String
On Error GoTo Handle:

cd.Filter = "Text Files (.txt)|*.txt"
cd.FileName = vbNullString
cd.DialogTitle = Title
cd.ShowOpen

If LenB(cd.FileName) = 0 Then
Exit Function
End If

LoadFile List, cd.FileName, clear
LoadListCD = cd.FileName
Exit Function
Handle:
Debug.Print "Err #" & Err.Number & " on line " & Erl & vbNewLine & Err.Description
End Function

Public Sub LoadFile(List As ListBox, Path As String, Optional clear As Boolean = True)
On Error GoTo eh:
If clear Then
List.clear
End If
Dim lx As String
Open Path For Input As #1
Do Until EOF(1)
Line Input #1, lx
List.AddItem lx
Loop
Close #1
Exit Sub
eh:
Debug.Print "LoadFile Error: " & Err.Number & " " & Err.Description & " [" & Erl & "]"
End Sub

Public Sub SaveFile(List As ListBox, Path As String, Optional clear As Boolean = False)
Dim x As Long
If clear Then
List.clear
End If
Open Path For Output As #1
For x = 0 To List.ListCount - 1 Step 1
Print #1, List.List(x)
Next x
Close #1
End Sub


No component:
Option Explicit
Dim cd As Object

Public Sub SaveList(List As ListBox, Optional Title As String = "Save List", Optional clear As Boolean = False)
On Error GoTo Handle:
Set cd = CreateObject("MSComDlg.CommonDialog")
cd.Filter = "Text Files (.txt)|*.txt"
cd.filename = vbNullString
cd.ShowSave
cd.DialogTitle = Title

If LenB(cd.filename) = 0 Then
Exit Sub
End If
SaveFile List, cd.filename
Exit Sub
Handle:
Debug.Print "Err #" & Err.Number & " on line " & Erl & vbNewLine & Err.Description
End Sub

Public Function LoadList(List As ListBox, Optional Title As String = "Load List", Optional clear As Boolean = True) As String
On Error GoTo Handle:
Set cd = CreateObject("MSComDlg.CommonDialog")
cd.Filter = "Text Files (.txt)|*.txt"
cd.filename = vbNullString
cd.DialogTitle = Title
cd.ShowOpen

If LenB(cd.filename) = 0 Then
Exit Function
End If

LoadFile List, cd.filename, clear
LoadListCD = cd.filename
Exit Function
Handle:
Debug.Print "Err #" & Err.Number & " on line " & Erl & vbNewLine & Err.Description
End Function

Public Sub LoadFile(List As ListBox, Path As String, Optional clear As Boolean = True)
On Error GoTo eh:
If clear Then
List.clear
End If
Dim lx As String
Open Path For Input As #1
Do Until EOF(1)
Line Input #1, lx
List.AddItem lx
Loop
Close #1
Exit Sub
eh:
Debug.Print "LoadFile Error: " & Err.Number & " " & Err.Description & " [" & Erl & "]"
End Sub

Public Sub SaveFile(List As ListBox, Path As String, Optional clear As Boolean = False)
Dim x As Long
If clear Then
List.clear
End If
Open Path For Output As #1
For x = 0 To List.ListCount - 1 Step 1
Print #1, List.List(x)
Next x
Close #1
End Sub