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
            End If
        End If
    End Sub

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.

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)
This article has been dead for over six months. Start a new discussion instead.