Read text from RichTextBox and save the file from save file dialog
Imports System.IO
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim FileSave As New SaveFileDialog()
FileSave.Filter = "All files | *.* | Text files | *.txt"
FileSave.FilterIndex = 2
FileSave.InitialDirectory = "C:\"
FileSave.AddExtension = True
FileSave.DefaultExt = "txt"
If (FileSave.ShowDialog() = Windows.Forms.DialogResult.OK) Then
Dim File As StreamWriter
Try
File = New StreamWriter(FileSave.FileName)
Catch
MsgBox("Error open/create " &
FileSave.FileName)
End Try
Dim i As Integer
Try
For i = 0 To Me.RichTextBox1.Text.Length - 1
If Me.RichTextBox1.Text.Chars(i)
= Chr(10) Then 'Chr(10)
= new line char
File.WriteLine()
Else
File.Write(Me.RichTextBox1.Text.Chars(i))
End If
Next
Catch
MsgBox("Error writing file")
End Try
File.Close()
MsgBox("Text saved to " &
FileSave.FileName)
Else
MsgBox("User selected Cancel")
End If
End Sub
End Class