How to save contents of a RichText Box to .rtf file in VB.Net without using Save dialog Box
In this article I am going to show you how you can save contents of a RichText Box to .rtf file and also load the contents of that .rtf file to Rich Text Box in VB.Net without using Save or open dialog Box.
In this article I am going to show you how you can save contents of a RichText Box to .rtf file and also load the contents of that .rtf file to Rich Text Box in VB.Net without using Save or open dialog Box.
First of all create a new project and add a Rich Textbox and three buttons on the form as shown in image.
Now, change the name of richtext box1 to richtextbox and also change the name of three buttons as btnsave, btnclear and btnload respectively. Open the code window of the form and write the following code on the click events of the buttons.
Note:- you need to add Imports System.IO in the general section of the code window.
Imports System.IO
Public Class Form1
Private Sub btnsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsave.Click
File.WriteAllText("C:\sam.rtf", RichTextBox.Text)
End Sub
Private Sub btnclear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnclear.Click
RichTextBox.Text = ""
End Sub
Private Sub btnload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnload.Click
RichTextBox.Text = File.ReadAllText("C:\sam.rtf")
End Sub
End Class
Finally press F5 to run the project.
Regards