ARTICLE

Working with RichTextBox Control in vb.net

Posted by Deepak Kumar Verma Articles | Visual Basic 2010 February 17, 2011
Here we learn how to format text in RichTextBox Control and save & load .rtf (Rich Text Format) file to/from HDD.
Download Files:
 
Reader Level:

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) Handles MyBase.Load
        RichTextBox1.Focus()
    End
Sub
End Class

Output:

Rich Text Box

 RichTextBox.gif

Login to add your contents and source code to this article
share this article :
post comment
 

it has some bugs like if you make selected text bold then when you write any thing else after it will get bold

Posted by Muhammad Yaseen Dec 18, 2011
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Team Foundation Server Hosting
Become a Sponsor