The RichTextBox Control in VB.Net
The RichTextBox allows formatting the text. It also
provides the facility to save the text in the file and restore it from the file.
Drag and drop RichTextBox control from the toolbox on the window Form.
Write the Text in RichTextBox
Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Find method is used to find the text written in RichTextBox
RichTextBox1.SelectionStart = RichTextBox1.Find("Hello")
RichTextBox1.SelectionColor = Color.BlueViolet
RichTextBox1.SelectionStart = RichTextBox1.Find("How are you")
RichTextBox1.SelectionColor = Color.DarkOliveGreen
End Sub
End Class
Run the Project
Save RichTextBox Text in File
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' SaveFile Method Saves the contents of the RichTextBox to a file.
RichTextBox1.SaveFile("hello.rtf")
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'LoadFile Method Loads the contents of a file into the RichTextBox control.
RichTextBox2.LoadFile("hello.rtf")
End Sub
Run the Project
When you click save button then written text will save to the specified file and in specified path.
When you click Load button then saved file will load in RichTextBox2
Leave Comment