0

Hello sir,
I am using VS2008. I am using save file dialogue box and i am saving the contents of richtextbox which is in the form. So i used the coding below. It only creating the empty file but not the text which i typed in the richtextbox.

Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim ab
        ab = RichTextBox1.Text
        Dim myStream As IO.Stream
        Dim saveFileDialog1 As New SaveFileDialog()

        saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
        saveFileDialog1.FilterIndex = 2
        saveFileDialog1.RestoreDirectory = True

        If saveFileDialog1.ShowDialog() = DialogResult.OK Then
            myStream = saveFileDialog1.OpenFile()
            If (myStream IsNot Nothing) Then
                ab = RichTextBox1.Text
                myStream.Close()
            End If
        End If
    End Sub
4
Contributors
3
Replies
4
Views
8 Years
Discussion Span
Last Post by Ramy Mahrous
0

This line ab = RichTextBox1.Text doesn't actually write anything to the file. You might want to replace that line with something that accesses myStream.

0

That's simply because you don't write anything to the stream!!

Here you're

ab = RichTextBox1.Text.Replace("\n", "\r\n") ' to keep new lines in text file like richbox
Dim stringAsBytes() As Byte
stringAsBytes = New System.Text.ASCIIEncoding().GetBytes(ab)
myStream.Write(stringAsBytes, 0, stringAsBytes.Length)
myStream.Close()
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.