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

Recommended Answers

All 3 Replies

are u sure saved it into txt file?i mean when your program is running..

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)
myStream.Close()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.