Results 1 to 8 of 8

Thread: Writing to a file

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2001
    Posts
    244

    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:
    1. Private Sub mnuSaveAs_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuSaveAs.Click
    2.         SaveFileDialog.Filter = "Text files (*.txt)|*.txt"
    3.         SaveFileDialog.ShowDialog()
    4.         If SaveFileDialog.FileName <> "" Then
    5.             FileOpen(1, SaveFileDialog.FileName, OpenMode.Output)
    6.             PrintLine(1, txtReport.Text)  'copy text to disk
    7.             FileClose(1)
    8.         End If
    9.     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

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    VB Code:
    1. Private Sub mnuSaveAs_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuSaveAs.Click
    2.         SaveFileDialog.Filter = "Text files (*.txt)|*.txt"
    3.         SaveFileDialog.ShowDialog()
    4.         If SaveFileDialog.FileName <> "" Then
    5.  
    6.              Dim sw As New IO.StreamWriter(New IO.FileStream(SaveFileDialog.FileName, IO.FileMode.Append))
    7.              sw.Write(txtReport.Text)
    8.              sw.Close()
    9.         End If
    10.     End Sub

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2001
    Posts
    244
    Everything in the multi-line text box is still saving to a single line in the text file.

  4. #4
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    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 .

  5. #5
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    Dunno why it would work different for you, for me it produces multiple lines in the file:
    VB Code:
    1. Dim fs As FileStream
    2.         Dim sw As StreamWriter
    3.         Try
    4.             fs = File.Open("c:\temp\foo.txt", FileMode.CreateNew)
    5.         Catch ex As Exception
    6.             Try
    7.                 fs = File.Open("c:\temp\foo.txt", FileMode.Truncate)
    8.             Catch ex2 As Exception
    9.                 MsgBox(ex.Message)
    10.             End Try
    11.         End Try
    12.  
    13.         Try
    14.             sw = New StreamWriter(fs)
    15.         Catch ex As Exception
    16.             MsgBox(ex.Message)
    17.         End Try
    18.  
    19.         sw.Write(TextBox1.Text)
    20.         sw.Flush()
    21.         sw.Close()
    22.         fs.Close()

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jan 2001
    Posts
    244
    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...?

  7. #7
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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.

  8. #8
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    To programatically format text in a richtextbox:

    VB Code:
    1. Dim fntNorm As New Font("Arial", 9, FontStyle.Regular)
    2.         Dim fntBold As New Font("Arial", 9, FontStyle.Bold)
    3.         Dim fntUnd As New Font("Arial", 9, FontStyle.Underline)
    4.  
    5.         RichTextBox1.SelectionFont = fntBold
    6.         RichTextBox1.AppendText("Foo!" & ControlChars.NewLine)
    7.         RichTextBox1.SelectionFont = fntUnd
    8.         RichTextBox1.AppendText("Test line!" & ControlChars.NewLine)
    9.         RichTextBox1.SelectionText = fntNorm

    Produces:

    Code:
    Foo!
    Test line!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width