Library code snippets
Load/Save listboxes to a file
Note
You can invoke this and the next two functions by starting on opening a form
use the Private Sub Form_Load()to initiate the loading of the values
to your form on start up, or call the function in a separate module by using
a command button.
Examples
'load the contents of listitems.txt into lstItems
Call LoadListFromFile(App.Path & "\listitems.txt",lstItems)
'save lstItems to listitems.txt, without clearing it
Call SaveListToFile(App.Path & "\listitems.txt",lstItems)
'save lstItems to listitems.txt, and clear it
Call SaveListToFile(App.Path & "\listitems.txt",lstItems,True)
Put the following code in a module
'Read data to a list.box form an ASCII file 'paste this in a module
Public Function LoadListFromFile(ByRef SourceFile As String, _ ByRef ToFormList As ListBox) On Error GoTo ErrEvt Dim TextLine As String, FN As Integer ToFormList.Clear FN = FreeFile Open SourceFile For Input As #FN ' Open file. Do While Not EOF(FN) ' Loop until end of file. Line Input #FN, TextLine ' Read line into variable. If TextLine <> LineToRem Then ToFormList.AddItem(TextLine) End If Loop Close #FN ' Close file. Exit Function 'this error handler will skip the nasty problem 'if your text file isnt conformed with an extra linebreak at the end 'to avoid saying INPUT PAST EOF ErrEvt: Select Case Err.Number Case 51 Err.Clear ' just bail out from this Case Else ' do nothing End Select Resume Next End Function
'============================================ 'Outputting list boxes to text files
'Call with blnClearList=true to clear the listbox afterwards Public Function SaveListToFile(ByVal strPrintToFile As String, _ ByRef lstFormList As ListBox, Optional ByVal blnClearList As Boolean = False) Dim I As Long 'longs are quicker than Integers so I normally use those Dim FN As Integer FN = FreeFile 'print each line in the list to a new text file Open strPrintToFile For Output As #FN 'Add all Items to the opened file For I = 0 To lstFormList.ListCount - 1 Print #FN, lstFormList.List(I) Next I Close #FN 'thats it... your file is updated
'clear the listbox?
If blnClearList = True Then lstFormList.Clear End Function '============================================
Related articles
Related discussion
-
VB6 Runtime error 381 subsript out of range Error
by Uncle (2 replies)
-
passing and reading parameters from using Shell
by jigartoliya (0 replies)
-
Convert C++ code to VB6
by mawcot (4 replies)
-
listbox scrollbar
by Dennijr (10 replies)
-
Can you describe Above simple VB6 code?
by pramodmca09 (0 replies)
Related podcasts
-
Christian Beauclair
14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...
Call LoadListFromFile(App.Path & "\listitems.txt",lstItems)
my error happens with the "&" between App.Path and
"\listitems.txt"For this code to work you may have to change the module code at the
This thread is for discussions of Load/Save listboxes to a file.