ARTICLE

How to format text in RichTextbox control using VB.NET

Posted by Dea Saddler Articles | ASP.NET using VB.NET March 21, 2011
In this article we demonstrate on what is RichTextbox control and how we format text in this control
Download Files:
 
Reader Level:

A RichTextbox is a visual basic control which is similar to Textbox but having some advance features in comparison to standard TextBox. You can add color, change color, formate text, with different font size and style by RichTextbox. this control permit you to create formatted text in Rich Text Format(RTF) after formatting the program can be read by other programs like MS Word and  WordPad. You can set RichTextBox control to detect URL information and respond to the user clicking the URL. You can also create your word processors with RichTextbox. RichTextbox have a default Event named TextChanged Event.
 
How to Format text in RichTextbox Control:-

  • Create a new project. 
  • Drag a RichTextbox and a button on form the form will look like below.

    Rich-Textbox1.gif
     
  • Write the below code behind button click.

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
           
    Button1.Click
            ' String 1
            Dim Position As Int16 = 0
            Dim string1 As String = "shalini. Font=Arial: FontSize=14: Color=Blue" & vbNewLine
            Dim myFont As New Font("Arial", 14, FontStyle.Regular, GraphicsUnit.Point)
            Dim myColor As Color = Color.Blue
            rtb.Select(Position, 0)
            rtb.SelectionFont = myFont
            rtb.SelectionColor = myColor
            rtb.SelectedText = string1
            Position += string1.Length
            'String 2
            string1 = "program. Font=microsoft sans serif: FontSize=10: Color=Red" & vbNewLine
            myFont = New Font("microsoft sans serif", 10, FontStyle.Regular, GraphicsUnit.Point)
            myColor = Color.Red
            rtb.Select(Position, 0)
            rtb.SelectionFont = myFont
            rtb.SelectionColor = myColor
            rtb.SelectedText = string1
            Position += string1.Length
            ' String 3
            string1 = "juneja. Font=courier new: FontSize=30: Color=Green: BOLD" & vbNewLine
            myFont = New Font("courier new", 30, FontStyle.Bold, GraphicsUnit.Point)
            myColor = Color.Green
            rtb.Select(Position, 0)
            rtb.SelectionFont = myFont
            rtb.SelectionColor = myColor
            rtb.SelectedText = string1
            Position += string1.Length
        End
    Sub

    Output:-

    Rich-Textbox2.gif

    Rich-Textbox3.gif

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

..perfectly. Thank you for sharing the knowledge and shedding light

Posted by Lenyora Makgopa Oct 13, 2011
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Become a Sponsor