|
-
May 21st, 2003, 11:26 AM
#1
Thread Starter
Addicted Member
Writing to a file
I have a multi-lined text box that users can save as a text file. Here is the code that I have:
VB Code:
Private Sub mnuSaveAs_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuSaveAs.Click
SaveFileDialog.Filter = "Text files (*.txt)|*.txt"
SaveFileDialog.ShowDialog()
If SaveFileDialog.FileName <> "" Then
FileOpen(1, SaveFileDialog.FileName, OpenMode.Output)
PrintLine(1, txtReport.Text) 'copy text to disk
FileClose(1)
End If
End Sub
Obviously it writes everything to a single line of text in the file. What is the easiest way around this so that it saves the the txt file the same way it appears on screen?
Thanks,
Jim
-
May 21st, 2003, 11:43 AM
#2
VB Code:
Private Sub mnuSaveAs_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuSaveAs.Click
SaveFileDialog.Filter = "Text files (*.txt)|*.txt"
SaveFileDialog.ShowDialog()
If SaveFileDialog.FileName <> "" Then
Dim sw As New IO.StreamWriter(New IO.FileStream(SaveFileDialog.FileName, IO.FileMode.Append))
sw.Write(txtReport.Text)
sw.Close()
End If
End Sub
-
May 21st, 2003, 11:46 AM
#3
Thread Starter
Addicted Member
Everything in the multi-line text box is still saving to a single line in the text file.
-
May 21st, 2003, 11:57 AM
#4
Sleep mode
The code Edneeis posted is working very well . Maybe your notepad Word wrap property is set to one line . Change it to false and see it may work for you .
-
May 21st, 2003, 11:58 AM
#5
Fanatic Member
Dunno why it would work different for you, for me it produces multiple lines in the file:
VB Code:
Dim fs As FileStream
Dim sw As StreamWriter
Try
fs = File.Open("c:\temp\foo.txt", FileMode.CreateNew)
Catch ex As Exception
Try
fs = File.Open("c:\temp\foo.txt", FileMode.Truncate)
Catch ex2 As Exception
MsgBox(ex.Message)
End Try
End Try
Try
sw = New StreamWriter(fs)
Catch ex As Exception
MsgBox(ex.Message)
End Try
sw.Write(TextBox1.Text)
sw.Flush()
sw.Close()
fs.Close()
-
May 21st, 2003, 12:05 PM
#6
Thread Starter
Addicted Member
My apologies. My code was working from the begining. I forgot I changed the TextBox to a RichTextBox. Now that it is saving and opening as an RTF it looks fine.
On another note does anyone know how to format text as it is being written to a RichTextBox? (Font, Size, Color, Alignment, etc...?
-
May 21st, 2003, 12:56 PM
#7
If you want to preserve the formatting then you shouldn't use the text property you should save the Rtf property and set it again when loading.
-
May 21st, 2003, 05:38 PM
#8
Fanatic Member
To programatically format text in a richtextbox:
VB Code:
Dim fntNorm As New Font("Arial", 9, FontStyle.Regular)
Dim fntBold As New Font("Arial", 9, FontStyle.Bold)
Dim fntUnd As New Font("Arial", 9, FontStyle.Underline)
RichTextBox1.SelectionFont = fntBold
RichTextBox1.AppendText("Foo!" & ControlChars.NewLine)
RichTextBox1.SelectionFont = fntUnd
RichTextBox1.AppendText("Test line!" & ControlChars.NewLine)
RichTextBox1.SelectionText = fntNorm
Produces:
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
|