Working with RichTextBox Control in vb.net
Here we learn how to format text in RichTextBox Control and save & load .rtf (Rich Text Format) file to/from HDD.
Rich Text Boxes are similar to Text Boxes but having some advance features. Rich Text Boxes allow us to do text formatting (eg. bold, italic, underline, color, etc.).
We can also make our own word processor by using RichTextBox Control in vb.net and the text written in RichTextBox can be saved and loaded. It supports ".rtf" extension.
Code :
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If RichTextBox1.SelectedText <> "" Then
RichTextBox1.SelectionStart = RichTextBox1.Find(RichTextBox1.SelectedText
Dim CurrentFont As Font = RichTextBox1.SelectionFont
Dim NewFontStyle As New FontStyle
With CurrentFont
NewFontStyle = -((Not .Bold) * FontStyle.Bold)
NewFontStyle += -(.Italic * FontStyle.Italic)
NewFontStyle += -(.Underline * FontStyle.Underline)
RichTextBox1.SelectionFont = New Font(CurrentFont, NewFontStyle)
End With
RichTextBox1.Focus()
End If
End Sub
Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles Button7.Click
End
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles Button2.Click
If RichTextBox1.SelectedText <> "" Then
RichTextBox1.SelectionStart = RichTextBox1.Find(RichTextBox1.SelectedText)
Dim CurrentFont As Font = RichTextBox1.SelectionFont
Dim NewFontStyle As New FontStyle
With CurrentFont
NewFontStyle = -((Not .Italic) * FontStyle.Italic)
NewFontStyle += -(.Bold * FontStyle.Bold)
NewFontStyle += -(.Underline * FontStyle.Underline)
RichTextBox1.SelectionFont = New Font(CurrentFont, NewFontStyle)
End With
RichTextBox1.Focus()
End If
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles Button3.Click
If RichTextBox1.Text <> "" Then
RichTextBox1.SelectionStart = RichTextBox1.Find(RichTextBox1.SelectedText)
Dim CurrentFont As Font = RichTextBox1.SelectionFont
Dim NewFontStyle As New FontStyle
NewFontStyle = -((Not CurrentFont.Underline) * FontStyle.Underline)
NewFontStyle += -(CurrentFont.Bold * FontStyle.Bold)
NewFontStyle += -(CurrentFont.Italic * FontStyle.Italic)
RichTextBox1.SelectionFont = New Font(CurrentFont, NewFontStyle)
End If
RichTextBox1.Focus()
End Sub
Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles Button11.Click
If RichTextBox1.Text <> "" Then
RichTextBox1.SelectionStart = RichTextBox1.Find(RichTextBox1.SelectedText)
RichTextBox1.SelectionFont = New Font(RichTextBox1.Font, FontStyle.Regular)
RichTextBox1.Focus()
End If
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles Button4.Click
If RichTextBox1.SelectedText <> "" Then
RichTextBox1.SelectionStart = RichTextBox1.Find(RichTextBox1.SelectedText)
RichTextBox1.SelectionColor = Color.Red
RichTextBox1.Focus()
End If
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles Button5.Click
If RichTextBox1.SelectedText <> "" Then
RichTextBox1.SelectionStart = RichTextBox1.Find(RichTextBox1.SelectedText)
RichTextBox1.SelectionColor = Color.Green
RichTextBox1.Focus()
End If
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles Button6.Click
If RichTextBox1.SelectedText <> "" Then
RichTextBox1.SelectionStart = RichTextBox1.Find(RichTextBox1.SelectedText)
RichTextBox1.SelectionColor = Color.Blue
RichTextBox1.Focus()
End If
End Sub
Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles Button8.Click
If RichTextBox1.SelectedText <> "" Then
RichTextBox1.SelectionStart = RichTextBox1.Find(RichTextBox1.SelectedText)
RichTextBox1.SelectionColor = Color.Black
RichTextBox1.Focus()
End If
End Sub
Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles Button10.Click
Try
RichTextBox1.LoadFile("File.rtf")
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles Button9.Click
Try
RichTextBox1.SaveFile("File.rtf")
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.Load
RichTextBox1.Focus()
End Sub
End Class
Output:
Rich Text Box
