473,408 Members | 1,861 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,408 software developers and data experts.

Writing listbox items into text file

Hi,
I can read from a text file and insert text file's contents into a
listbox whose format is line by line using this code:

Dim reader As String
reader = My.Computer.FileSystem.ReadAllText("c:
\bookmarks.txt")

Dim strs() As String
strs = Split(reader, Environment.NewLine)
For Each s As String In strs
ListBox1.Items.Add(s)
Next
But i also want to write all listbox items into text file "line by
line"? How can i do this?

I tried My.Computer.FileSystem.WriteAlltext but couldn't make out
completely.

I hope i learn.

Thanks!
Dec 29 '07 #1
6 15397
Hi kimiraikkonen,

use this if you like:
TextWriter.WriteLine Method.

Check the Stream Classes and their derivates
that come with write line method. You also can
use plain streams of bytes and byte arrays with
your own line feeds and breaks. But if you dont
want to implement this use the already available
write line methods,...

Regards

Kerem

--
-----------------------
Beste Grüsse / Best regards / Votre bien devoue
Kerem Gümrükcü
Microsoft Live Space: http://kerem-g.spaces.live.com/
Latest Open-Source Projects: http://entwicklung.junetz.de
-----------------------
"This reply is provided as is, without warranty express or implied."
Dec 29 '07 #2
On Dec 29, 4:14 pm, "Kerem Gümrükcü" <kareem...@hotmail.comwrote:
Hi kimiraikkonen,

use this if you like:
TextWriter.WriteLine Method.

Check the Stream Classes and their derivates
that come with write line method. You also can
use plain streams of bytes and byte arrays with
your own line feeds and breaks. But if you dont
want to implement this use the already available
write line methods,...

Regards

Kerem

--
-----------------------
Beste Grüsse / Best regards / Votre bien devoue
Kerem Gümrükcü
Microsoft Live Space:http://kerem-g.spaces.live.com/
Latest Open-Source Projects:http://entwicklung.junetz.de
-----------------------
"This reply is provided as is, without warranty express or implied."
Please sample some code. There must be some ways to save all listbox
items as a text file using with my.computer.filesystem.writealltext
classes instead of using Stream Writer's "writeline" method.

I tried something with "for each" but it only saves one(last) item if
append is false.

For Each i As String In ListBox1.Items
My.Computer.FileSystem.WriteAllText("c:\test.txt",
environment.newline & i, False)
Next

If append is true, previous + current items are saved into text file.
But i want current (last updated) items to be saved in a text file.
Shorly, i want to save what and how i see in listbox items as text
file.
Dec 29 '07 #3
"kimiraikkonen" <ki*************@gmail.comschrieb:
I can read from a text file and insert text file's contents into a
listbox whose format is line by line using this code:

Dim reader As String
reader = My.Computer.FileSystem.ReadAllText("c:
\bookmarks.txt")

Dim strs() As String
strs = Split(reader, Environment.NewLine)
For Each s As String In strs
ListBox1.Items.Add(s)
Next
='ListBox1.Items.AddRange(strs)'.
But i also want to write all listbox items into text file "line by
line"? How can i do this?

I tried My.Computer.FileSystem.WriteAlltext but couldn't make out
completely.
Take a look at the 'Join' function.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Dec 29 '07 #4
On Dec 29, 4:25 pm, "Herfried K. Wagner [MVP]" <hirf-spam-me-
h...@gmx.atwrote:
"kimiraikkonen" <kimiraikkone...@gmail.comschrieb:
I can read from a text file and insert text file's contents into a
listbox whose format is line by line using this code:
Dim reader As String
reader = My.Computer.FileSystem.ReadAllText("c:
\bookmarks.txt")
Dim strs() As String
strs = Split(reader, Environment.NewLine)
For Each s As String In strs
ListBox1.Items.Add(s)
Next

='ListBox1.Items.AddRange(strs)'.
But i also want to write all listbox items into text file "line by
line"? How can i do this?
I tried My.Computer.FileSystem.WriteAlltext but couldn't make out
completely.

Take a look at the 'Join' function.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Could you help to code using "join" to save all listbox items in a
textfile line by line?

Thanks
Dec 29 '07 #5
"kimiraikkonen" <ki*************@gmail.comschrieb:
Could you help to code using "join" to save all listbox items in a
textfile line by line?
Untested:

\\\
Dim ItemArray(Me.ListBox1.Items.Count - 1) As Object
Me.ListBox1.Items.CopyTo(ItemArray, 0)
Dim Data As String = Join(ItemArray, Environment.NewLine)
File.WriteAllText(<path>, Data)
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Dec 29 '07 #6
On Dec 29, 5:41 pm, "Herfried K. Wagner [MVP]" <hirf-spam-me-
h...@gmx.atwrote:
"kimiraikkonen" <kimiraikkone...@gmail.comschrieb:
Could you help to code using "join" to save all listbox items in a
textfile line by line?

Untested:

\\\
Dim ItemArray(Me.ListBox1.Items.Count - 1) As Object
Me.ListBox1.Items.CopyTo(ItemArray, 0)
Dim Data As String = Join(ItemArray, Environment.NewLine)
File.WriteAllText(<path>, Data)
///
Modified as:

Dim ItemArray(Me.ListBox1.Items.Count - 1) As Object
Me.ListBox1.Items.CopyTo(ItemArray, 0)
Dim Data As String = Join(ItemArray, Environment.NewLine)
My.Computer.FileSystem.WriteAllText("c:\test.txt", Data, False)

and worked! Thanks.
Dec 29 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: yamne | last post by:
I have a problem. When I click in edit datagrid button I show two listbox and two button. I use two button to move data between two listbox. My problem is that I can't call the listbox in the...
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..
14
by: Paul_Madden via DotNetMonster.com | last post by:
Basically I have a listbox to which I add simple STRING items- I have a progress bar which I increment whenever I populate another portion of the complete set of items I wish to add. What I observe...
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...
2
by: ProgrammerChicago | last post by:
I'm not sure if this is the result of the postback behavior or my own code, but for some reason my onclick function is not detecting listbox selections (It's meant to delete files uploaded to the...
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...
6
by: kimiraikkonen | last post by:
Hi, I want to save all the item content of a listbox line by line into a simple text file then recall them when my project is opened. For example listbox1 contains: That - item1 Group ...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.