|
-
Jan 7th, 2006, 02:53 PM
#1
Thread Starter
Addicted Member
Load/Save listbox data to .txt...[RESOLVED]
How do I do this? I want the user to be able to "save as" and not just save it to a file already specified.
Last edited by abstrait; Jan 7th, 2006 at 04:34 PM.
-
Jan 7th, 2006, 02:55 PM
#2
Re: Load/Save listbox data to .txt
Use the commondialog, you will find it in your components.
casey.
-
Jan 7th, 2006, 03:00 PM
#3
Thread Starter
Addicted Member
Re: Load/Save listbox data to .txt
But what is the vb code I have to use for the command button?
-
Jan 7th, 2006, 03:09 PM
#4
Re: Load/Save listbox data to .txt
try this
VB Code:
Dim str1 As String, i As Integer
On Error GoTo errorhandler
With CommonDialog1
.CancelError = True
.DialogTitle = "Select Location to Save to"
.Filter = "Text File (*.txt)|*.txt"
.InitDir = App.Path
.ShowSave
str1 = .FileName
End With
Open str1 For Output As #1
For i = 0 To List1.ListCount - 1
Print #1, List1.List(i)
Next i
Close #1
Exit Sub
errorhandler:
End Sub
you could also set some flags with the commondialog, have a look in your help files.
casey.
-
Jan 7th, 2006, 03:25 PM
#5
Re: Load/Save listbox data to .txt
Command1 Loads you file to Listbox, Command2 Saves your file to text file..
You choose file to open and file to save
VB Code:
Private Function FileOpen() As String
With CommonDialog1
On Error GoTo Err_Handler
.Filter = ".TXT Files .txt|*.txt"
.InitDir = App.Path
.Flags = &H1000 Or &H4 Or &H2000
.CancelError = True
.ShowOpen
FileOpen = .FileName
End With
Exit Function
Err_Handler:
End Function
Private Function FileSaveAs() As String
On Error GoTo Err_Handler
With CommonDialog1
.Filter = ".TXT Files .txt|*.txt"
.InitDir = App.Path
.Flags = &H2 Or &H4 Or &H2000
.CancelError = True
.ShowSave
FileSaveAs = .FileName
End With
Exit Function
Err_Handler:
End Function
Private Sub Command1_Click()
Dim lFile As String
Dim lFileContent As String
Dim lArrFile() As String
Dim i As Long
List1.Clear
lFile = FileOpen
If Trim$(lFile) <> vbNullString Then
Open lFile For Binary As #1
lFileContent = Space(LOF(1))
Get #1, , lFileContent
Close #1
lArrFile = Split(lFileContent, vbNewLine)
For i = 0 To UBound(lArrFile)
List1.AddItem lArrFile(i)
Next
End If
lFileContent = Empty
Erase lArrFile
End Sub
Private Sub Command2_Click()
Dim lStrFile As String
Dim i As Long
lStrFile = FileSaveAs
If Trim$(lStrFile) <> vbNullString Then
Open lStrFile For Output As #1
For i = 0 To List1.ListCount - 1
Print #1, List1.List(i)
Next
Close #1
End If
End Sub
Last edited by jcis; Jan 7th, 2006 at 03:41 PM.
-
Jan 7th, 2006, 03:35 PM
#6
Thread Starter
Addicted Member
Re: Load/Save listbox data to .txt
-
Jan 7th, 2006, 04:05 PM
#7
Thread Starter
Addicted Member
Re: Load/Save listbox data to .txt... One last question
How would I save a textbox? Nearly the same code, I assume. I just don't know what to change ListCount from
VB Code:
For i = 0 To Text2.ListCount - 1
Help?
-
Jan 7th, 2006, 04:08 PM
#8
Re: Load/Save listbox data to .txt... One last question
you wouldnt need the loop.
Print #1, Text1.Text
casey.
-
Jan 7th, 2006, 04:08 PM
#9
Re: Load/Save listbox data to .txt... One last question
Textboxes doesn't have a listcount, save its value directly, like:
Print #1, Text2.Text
-
Jan 7th, 2006, 04:24 PM
#10
Thread Starter
Addicted Member
Re: Load/Save listbox data to .txt... One last question
That works, thanks.
How would I clear three textboxes and two ListBoses with one click?
-
Jan 7th, 2006, 04:28 PM
#11
Re: Load/Save listbox data to .txt... One last question
For textboxes:
Text1.Text = VbNullString
For Listboxes:
List1.Clear
-
Jan 7th, 2006, 04:34 PM
#12
Thread Starter
Addicted Member
Re: Load/Save listbox data to .txt... One last question
Wonderful. That was my last question, I swear.
Thanks for all your help.
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
|
Click Here to Expand Forum to Full Width
|