-
VB6 from listbox to text file
Hello. I have a listbox that is populated from a txt file. The user has entered additional data into. I would like to write that back into the ini file. Having a bit of a hard time doing it sadly.
Can anyone help me with this?
Code:
Private Sub Command1_Click()
ListControl.AddItem Form1.Text1.Text
End Sub
Private Sub Command2_Click()
PopulateFileFromListControl ListControl, "c:\dave\file.txt"
End Sub
Private Sub Form_Load()
PopulateListControlFromFile ListControl, "c:\dave\file.txt"
End Sub
Public Function FileList() As Variant
Dim sWkg As String
Dim sAns() As String
Dim lCtr As Long
Dim dave As String
ReDim sAns(0) As String
Call getdave(dave)
sWkg = Dir(dave, vbNormal)
Do While Len(sWkg)
If sAns(0) = "" Then
sAns(0) = sWkg
Else
lCtr = UBound(sAns) + 1
ReDim Preserve sAns(lCtr) As String
sAns(lCtr) = sWkg
End If
sWkg = Dir
Loop
FileList = sAns
End Function
Public Function getdave(ByRef dave As String) As String
Dim mytxt As String
mytxt = Form1.Combo1.Text
dave = "C:\models\pds\drv\" & mytxt & "\*.*"
End Function
Private Sub cmdGo_click()
Dim retval As Variant
Dim obj
Open "c:\models\pds\drv\navlog.txt" For Append As #1
retval = FileList 'Assign the return value to a Variant Variable
For Each obj In retval
Print #1, obj & " " & Now
Next
Close #1
End Sub
Public Function PopulateListControlFromFile( _
ListControl As Object, FullPath As String, _
Optional Delimiter As String = vbCrLf) _
As Boolean
Dim objfso As New Scripting.FileSystemObject
Dim objTextStream As Scripting.TextStream
Dim strContents As String
Dim arrContents() As String
Dim lCtr As Long, lCount As Long
On Error GoTo ErrHandler:
'Ensure file exists
If Dir(FullPath) = "" Then Exit Function
'Clear List Control.
ListControl.Clear
'Get file contents
Set objTextStream = objfso.OpenTextFile(FullPath, ForReading)
strContents = objTextStream.ReadAll
objTextStream.Close
'Split file contents based on delimiter
arrContents = Split(strContents, Delimiter)
lCount = UBound(arrContents)
'Populate list control
For lCtr = 0 To lCount
ListControl.AddItem arrContents(lCtr)
Next
'Select first item (Optional, comment out
'if not desired
If ListControl.ListCount > 0 Then
ListControl.ListIndex = 0
End If
PopulateListControlFromFile = True
ErrHandler:
Set objTextStream = Nothing
Set objfso = Nothing
End Function
Public Function PopulateFileFromListControl( _
ListControl As Object, FullPath As String, _
Optional Delimiter As String = vbCrLf) _
As Boolean
Dim objfso As New Scripting.FileSystemObject
Dim objTextStream As Scripting.TextStream
Dim strContents As String
Dim arrContents() As String
Dim lCtr As Long, lCount As Long
On Error GoTo ErrHandler:
'Ensure file exists
If Dir(FullPath) = "" Then Exit Function
'Get file contents
Set objTextStream = objfso.OpenTextFile(FullPath, ForWriting)
'strContents = objTextStream.WriteLine
objTextStream.Close
'Split file contents based on delimiter
arrContents = Split(strContents, Delimiter)
lCount = ListControl.Items.Count
'Populate list control
For lCtr = 0 To lCount
objTextStream.WriteLine (lCtr)
Next
'Select first item (Optional, comment out
'if not desired
If ListControl.ListCount > 0 Then
ListControl.ListIndex = 0
End If
PopulateFileFromListControl = True
ErrHandler:
Set objTextStream = Nothing
Set objfso = Nothing
End Function
-
This is a routine I wrote many years ago and have uses numerous times. It deals with text files (which is, essentially, what an .InI file is), but you should be able to modify it to fit your needs. One routine that does both the saving and loading. It all depends on how you call it.
Code:
Private Sub SaveLoadListbox(plstLB As ListBox, pstrFileName As String, _
pstrSaveOrLoad As String)
Dim strListItems As String
Dim i As Long
Select Case pstrSaveOrLoad
Case "save"
Open pstrFileName For Output As #1
For i = 0 To plstLB.ListCount - 1
plstLB.Selected(i) = True
Print #1, plstLB.List(plstLB.ListIndex)
Next
Close #1
Case "load"
plstLB.Clear
Open pstrFileName For Input As #1
While Not EOF(1)
Line Input #1, strListItems
plstLB.AddItem strListItems
Wend
Close #1
End Select
End Sub
Private Sub Form_Load()
Call SaveLoadListbox(List1, "c:\Listbox.txt", "load")
End Sub
I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
Please use [Code]your code goes in here[/Code] tags when posting code.
Before posting your question, did you look here?
Got a question on Linux? Visit our Linux sister site.
Modifications Required For VB6 Apps To Work On Vista
-
Thanks for the help Hack. Yes I actually found this exact routine in a google search. I have stripped it down to this:
Code:
pstrFileName = "c:\models\pds\drv\paths.ini"
Open pstrFileName For Output As #1
For i = 0 To ListControl.ListCount - 1
ListControl.Selected(i) = True
Print #1, ListControl.List(ListControl.ListIndex)
Next
Close #1
Works great except that when it writes the ini, it leaves a blank space. Do you know why?
My end goal is that I would like for the ini to say:
Project: 1234567
LocalProjectPath: "c:\models\"
RemoteProjectPath: "T:models"
copyExt1:".exe"
copyExt2:".ini"
...like that. And then have VB read each of those values into their proper variables.
-
 Originally Posted by davetyner
Thanks for the help Hack. Yes I actually found this exact routine in a google search.
You found my routine on Goggle? Cool. I have posted it on other forums many, many times.
 Originally Posted by davetyner
I have stripped it down to this:
Code:
pstrFileName = "c:\models\pds\drv\paths.ini"
Open pstrFileName For Output As #1
For i = 0 To ListControl.ListCount - 1
ListControl.Selected(i) = True
Print #1, ListControl.List(ListControl.ListIndex)
Next
Close #1
Works great except that when it writes the ini, it leaves a blank space. Do you know why?
My end goal is that I would like for the ini to say:
Project: 1234567
LocalProjectPath: "c:\models\"
RemoteProjectPath: "T:models"
copyExt1:".exe"
copyExt2:".ini"
...like that. And then have VB read each of those values into their proper variables.
No, I don't know why it leaves a blank line.
Take the space out of the For/Next loop. See if that has any effect.
I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
Please use [Code]your code goes in here[/Code] tags when posting code.
Before posting your question, did you look here?
Got a question on Linux? Visit our Linux sister site.
Modifications Required For VB6 Apps To Work On Vista
Similar Threads
-
By praveenholal in forum C++
Replies: 3
Last Post: 03-17-2007, 12:09 AM
-
Replies: 1
Last Post: 01-12-2006, 06:42 PM
-
By George Gilbert in forum vb.announcements
Replies: 0
Last Post: 08-19-2001, 11:34 AM
-
By Paul in forum Database
Replies: 0
Last Post: 09-02-2000, 08:18 PM
-
By deborah in forum authorevents.kurata
Replies: 0
Last Post: 04-17-2000, 01:33 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks