It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming Visual Basic Forum

Text File

Moderator: dafunkymunky

Text File

Postby Darren on Tue May 03, 2011 12:36 pm

Hi ,
I have a form that has a rich text boxes that write to a txt file It works but the two lines in the text file are in one long line How can a make the code write to seperate lines
Code

Dim sfd As New SaveFileDialog
sfd.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"
sfd.ShowDialog()
Dim writer As New System.IO.StreamWriter(sfd.FileName)
writer.Write(RichTextBox1.Text)
writer.Write(RichTextBox2.Text)
writer.Close()
Darren
 
Posts: 13
Joined: Tue May 03, 2011 12:30 pm

Re: Text File

Postby jblues on Tue May 03, 2011 1:59 pm

try this

writer.Write(RichTextBox1.Text)
writer.Write(vbNewline)
writer.Write(RichTextBox2.Text)
jblues
 
Posts: 13
Joined: Tue May 03, 2011 11:08 am

Re: Text File

Postby Darren on Tue May 03, 2011 2:08 pm

jblues
Fantastic It works beautiful Many thanks appreciate your time in helping me .
If I may ask another question how can I extend the code so page 2 contents get added to the same text file without over writing it .
Darren
 
Posts: 13
Joined: Tue May 03, 2011 12:30 pm

Re: Text File

Postby jblues on Tue May 03, 2011 2:14 pm

Something like this:

objStreamWriter = System.IO.File.AppendText _
("file")

objStreamWriter.WriteLine _
(RichTextBox1.Text & vbNewline & RichTextBox2.Text)

objStreamWriter.Close()
jblues
 
Posts: 13
Joined: Tue May 03, 2011 11:08 am

Re: Text File

Postby Darren on Tue May 03, 2011 2:32 pm

jblues ,
Thanks once again for your help Does this code get added to existing one above or is it completely on it's own I tried it on it's own and the debug through this error message up
Error 1 'StreamWriter' is a type in 'IO' and cannot be used as an expression.
Error 2 'objStreamWriter' is not declared. It may be inaccessible due to its protection level.
Error 3 'objStreamWriter' is not declared. It may be inaccessible due to its protection level. Thanks
Darren
 
Posts: 13
Joined: Tue May 03, 2011 12:30 pm

Re: Text File

Postby jblues on Tue May 03, 2011 2:55 pm

declare the streamwriter

Dim objStreamWriter As System.IO.StreamWriter
jblues
 
Posts: 13
Joined: Tue May 03, 2011 11:08 am

Re: Text File

Postby Darren on Tue May 03, 2011 4:04 pm

jblues,
It is very kind of you to take the time to reply,but may I ask is this a seperate code or added to the one above Thank you appreciate your time Regards
Darren
 
Posts: 13
Joined: Tue May 03, 2011 12:30 pm

Re: Text File

Postby jblues on Tue May 03, 2011 4:31 pm

use the declaration code before the other code so

Dim sfds As New SaveFileDialog
sfds.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"
sfds.ShowDialog()

Dim objStreamWriter As System.IO.StreamWriter(sfds.FileName)

objStreamWriter = System.IO.File.AppendText _
("file") ' this is where you put the name of the file.

objStreamWriter.WriteLine _
(RichTextBox1.Text & vbNewline & RichTextBox2.Text)

objStreamWriter.Close()
jblues
 
Posts: 13
Joined: Tue May 03, 2011 11:08 am

Re: Text File

Postby Darren on Tue May 03, 2011 5:01 pm

jblues,
I have applied the code as you suggested this is the error message Plus no text file was created
Error 2 'objStreamWriter' is not declared. It may be inaccessible due to its protection level.
Plus it had a squiddly line under (sfd.FileName) Array bounds cannot appear
I'm sorry I am hard work but this is a steep learning curve for me sorry to be a pain Regards
Darren
 
Posts: 13
Joined: Tue May 03, 2011 12:30 pm

Re: Text File

Postby Darren on Tue May 03, 2011 5:05 pm

jblues,
So you can understand what I am trying to make is a two page form
First Page First Name
Surname
Age
Gender
Second Page
First Record you bought
Last record you bought
All sent to a text file
Regards
Darren
 
Posts: 13
Joined: Tue May 03, 2011 12:30 pm

Next

Who is online

Users browsing this forum: No registered users and 1 guest