473,394 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

How do you save ListBox Text to FIle

Ok well this is the script that i am using to save a normal textbox.

Expand|Select|Wrap|Line Numbers
  1. Private Sub CmdAdd_Click()
  2. Dim strNameScript As String
  3.  strNameScript = "RuneScapePalScript" + FrmScripts2.TxtScriptName.Text
  4.  Open strNameScript For Output As #1
  5.  Print #1, FrmScripts2.TxtScripty.Text
  6.  Close #1
  7. ErrHandler:
  8. End Sub
But i am having a lot of trouble finding a way that i can save ListBoxes... i can manage to get it to save one line from the list box but not all of the lines. If anyone can help me out here i would greatly appreciateit.. Thanks.
Jul 31 '07 #1
14 2402
hariharanmca
1,977 1GB
Ok well this is the script that i am using to save a normal textbox.

Expand|Select|Wrap|Line Numbers
  1. Private Sub CmdAdd_Click()
  2. Dim strNameScript As String
  3.  strNameScript = "RuneScapePalScript" + FrmScripts2.TxtScriptName.Text
  4.  Open strNameScript For Output As #1
  5.  Print #1, FrmScripts2.TxtScripty.Text
  6.  Close #1
  7. ErrHandler:
  8. End Sub
But i am having a lot of trouble finding a way that i can save ListBoxes... i can manage to get it to save one line from the list box but not all of the lines. If anyone can help me out here i would greatly appreciateit.. Thanks.


Expand|Select|Wrap|Line Numbers
  1. Private Sub CmdAdd_Click()
  2.     Dim strNameScript As String
  3.     strNameScript = "RuneScapePalScript" + FrmScripts2.TxtScriptName.Text
  4.     For i = 1 To <ListBox.ListCount> ' i am not sure about get listbox list count (<ListBox.ListCount>)
  5.         Open strNameScript For Output As #1
  6.             Print #1, FrmScripts2.ListBox.List(i)
  7.         Close #1
  8.     Next i
  9. ErrHandler:
  10.     <E.Handler ode>
  11. End Sub
Try the above code. I think this will help you
Jul 31 '07 #2
Im Still having trouble getting it to work i made myself a little program just to test everything, and heres the code from it,, it works fine. but when i open up the saved file in notepad It does not show anything that should of been saved

heres the script

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command1_Click()
  2.  List1.AddItem (Text1.Text)
  3.     Dim strNameScript As String
  4.     On Error GoTo ErrHandler
  5.     strNameScript = "Test"
  6.     For i = 1 To List1.ListCount
  7.         Open strNameScript For Output As #1
  8.             Print #1, List1.List(i)
  9.         Close #1
  10.     Next i
  11. ErrHandler:
  12. End Sub
Aug 1 '07 #3
fplesco
82
Im Still having trouble getting it to work i made myself a little program just to test everything, and heres the code from it,, it works fine. but when i open up the saved file in notepad It does not show anything that should of been saved

heres the script

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command1_Click()
  2.  List1.AddItem (Text1.Text)
  3.     Dim strNameScript As String
  4.     On Error GoTo ErrHandler
  5.     strNameScript = "Test"
  6.     For i = 1 To List1.ListCount
  7.         Open strNameScript For Output As #1
  8.             Print #1, List1.List(i)
  9.         Close #1
  10.     Next i
  11. ErrHandler:
  12. End Sub
Try this VincentWiebe -

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command1_Click()
  2.     List1.AddItem ("1")
  3.     List1.AddItem ("2")
  4.     Dim strNameScript As String
  5.     On Error GoTo ErrHandler
  6.     strNameScript = "Test"
  7.     For i = 0 To List1.ListCount - 1
  8.         Open strNameScript For Output As #1
  9.             Print #1, List1.List(i)
  10.         Close #1
  11.     Next i
  12. ErrHandler:
  13. End Sub
You might have confused with the array index. It suppose to start at 0 and it happens that you have only 1 test data. Maybe that explains why your file doesnt have text on it.

Godspeed!
Aug 1 '07 #4
fplesco
82
Hello there VincentWiebe -

Please disregard my first post. Try this instead.

I put the opening of the file
Open strNameScript For Output As #1
and closing of the file
Close #1
outside the For statement block.
Expand|Select|Wrap|Line Numbers
  1. Private Sub Command1_Click()
  2.     List1.AddItem ("1")
  3.     List1.AddItem ("2")
  4.     Dim strNameScript As String
  5.     On Error GoTo ErrHandler
  6.     strNameScript = "Test"
  7.     Open strNameScript For Output As #1
  8.     For i = 0 To List1.ListCount - 1    
  9.            Print #1, List1.List(i)
  10.     Next i
  11.     Close #1
  12. ErrHandler:
  13. End Sub
You might have confused with the array index. It suppose to start at 0 and it happens that you have only 1 test data. Maybe that explains why your file doesnt have text on it.

Godspeed!
Aug 1 '07 #5
Killer42
8,435 Expert 8TB
The main problem was that opening and closing the file each time around the loop caused the data to be overwritten each time. Of course, the loop does need to start and finish at the right values.
Aug 1 '07 #6
hariharanmca
1,977 1GB
The main problem was that opening and closing the file each time around the loop caused the data to be overwritten each time. Of course, the loop does need to start and finish at the right values.
yha and sorry i missed that(opening and closing the file each time around the loop).
Aug 1 '07 #7
Alright Thanks a lot guys

It Works prefectly, but before i get all confused as to make it load i was wondering if there is a certain way i have to do that as well?

Thanks
Aug 2 '07 #8
Killer42
8,435 Expert 8TB
Not sure what you mean about loading.

However, here's a simple Sub you can place in a code module and call from anywhere...

Expand|Select|Wrap|Line Numbers
  1. Public Sub SaveListBox(ByVal Src As ListBox, ByVal FileName As String)
  2.   Dim FileNum As Long, I As Long
  3.   FileNum = FreeFile
  4.   Open FileName For Output As #FileNum
  5.   With Src
  6.     For I = 0 To .ListCount - 1
  7.       Print #FileNum, .List(I)
  8.     Next
  9.   End With
  10.   Close #FileNum
  11. End Sub
Aug 2 '07 #9
fplesco
82
Alright Thanks a lot guys

It Works prefectly, but before i get all confused as to make it load i was wondering if there is a certain way i have to do that as well?

Thanks
If you need to OPEN and CLOSE the file several times in the loop and you want content of the FILE not overwritten, you can use APPEND.
Aug 2 '07 #10
Sorry by load i mean, i want it to put the text, from the file i saved previously, into the list box.

Herses the script i use to load/open, a file and put the text that is in the file into a textbox

Private Sub Form_Load()
Dim strNamePassword As String, strFilePassword As String, strTempPassword As String
On Error GoTo ErrHandler
strNamePassword = "RuneScapePalPassword"
Open strNamePassword For Input As #1
strFilePassword = ""
Do Until EOF(1)
Line Input #1, strTempPassword
strFilePassword = strFilePassword & strTempPassword & vbCrLf
Loop
TxtPassHide.Text = strFilePassword
Close #1
ErrHandler:
If TxtPassHide.Text = "" Then
FrmLogin.Hide
FrmUserData.Show
End If
End Sub

I'm just not sure how to do this with a listbox seeing how there are multiple lines of text.
Aug 2 '07 #11
Killer42
8,435 Expert 8TB
Just do an AddItem for each line. And in this case, skip the vbCrLf.
Aug 2 '07 #12
I tried a bunch of different things but i cant get it to work.
Aug 2 '07 #13
Alright i did a bit of messing around and found something that works, here it is..

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load()
  2. Dim strNameListScript As String, strFileListScript As String, strTempListScript As String
  3. On Error GoTo ErrHandler
  4. strNameListScript = "RuneScapePalListScript"
  5. Open strNameListScript For Input As #1
  6. Do Until EOF(1)
  7.  strFileListScript = ""
  8.  Line Input #1, strTempListScript
  9.  strFileListScript = strFileListScript & strTempListScript
  10.  LstScripts.AddItem (strFileListScript)
  11. Loop
  12. Close #1
  13. ErrHandler:
  14. End Sub
Works Perfectly, thanks for all the help everyone
Aug 2 '07 #14
Killer42
8,435 Expert 8TB
... Works Perfectly
It does? To be honest, I'm very surprised. Because it looks to me as though you are adding each line onto the end of the previous one, and then sticking the ever-lengthening text into the listbox. In other words, let us assume the text file contained...
A
B
C
D
I would expect this code to produce the following in the listbox...
A
AB
ABC
ABCD
Here's a modified copy, which is closer to what I would have done...
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load()
  2.   Dim strNameListScript As String, strTempListScript As String
  3.   On Error GoTo ErrHandler
  4.   strNameListScript = "RuneScapePalListScript"
  5.   Open strNameListScript For Input As #1
  6.   Do Until EOF(1)
  7.     Line Input #1, strTempListScript
  8.     LstScripts.AddItem (strTempListScript)
  9.   Loop
  10.   Close #1
  11.   ErrHandler:
  12. End Sub
Ah! My mistake. I missed the fact that you were clearing the value of strFileListScript each time around the loop. Which is a bit pointless, really. Why clear out a string, move the other one into it, then use that? Just use the original one.
Aug 2 '07 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Andras Gilicz | last post by:
Hi VB fans I'm working on a relatively large project in VB6 with about a dozen forms, including graphs, labels, text boxes, etc. The software itself is actually a flow simulator with more or...
2
by: mathieu cupryk | last post by:
I have problems with listboxes in the webform2.cs, the textboxes are working well when I do a click on next. I am missing something. It works with the textboxes. Here is the file: using System;...
7
by: Ohad Asor | last post by:
Hello all, I have an ASP.NET page I've written using VS.NET2003, which have a ListBox in it. When I press a button in the form, I try to get the selected item in the list by calling...
3
by: Ali Chambers | last post by:
Hi, I have created a listbox called "dtlist1" on my VB.NET form. I call a procedure as follows: Private Sub openfile(flname As String) dtlist1.Items.Clear() etc..
2
by: rn5a | last post by:
A ListBox lists all the files & directories existing in a directory on the server. If an item in the ListBox happens to be a directory, then the name of the directory is appended with the text ....
4
by: rn5a | last post by:
Can the items in a ListBox be sorted by the name of the items? The ListBox actually lists all directories & files existing in a directory on the server. Note that all the directories should be...
5
by: VBRAZORBACK | last post by:
I am trying to take the values in a listbox and save them to a txt file. So far I am getting nowhere.
1
by: Sunray | last post by:
I have a form called the sales form and i have 2 sets of listboxes So what happens is. i add items form the bottom set of list boxes which are bound to a data base to the top set of list boxes which...
1
by: papafreebird | last post by:
I would like to save the contents of a wxListBox to a text file. Using borland c++ builder I am able to accomplish this by using the following code ListBox1->Items->GetText(); ...
2
by: simonyong | last post by:
Hello, anyone I had search for few days with how to save file when user choose a file name from listbox and i will search the file from database and user can save it into their desktop what I had...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.