Search | Contact | Link To Us  

      VB .NET       

.NET Defined
OOP with VB
VB Language
Win Forms
Windows Controls
ADO .NET
User Controls
File Handling
Multithreading
Deployment
XML Web Services Essential XML
Resources
Discussions
ASP.NET
About



Advertisement


     Home> Windows Controls> RichTextBox

RichTextBox

RichTextBoxes are similar to TextBoxes but they provide some advanced features over the standard TextBox. RichTextBox allows formatting the text, say adding colors, displaying particular font types and so on. The RichTextBox, like the TextBox is based on the TextBoxBase class which is based on the Control class. These RichTextBoxes came into existence because many word processors these days allow us to save text in a rich text format. With RichTextBoxes we can also create our own word processors. We have two options when accessing text in a RichTextBox, text and rtf (rich text format). Text holds text in normal text and rtf holds text in rich text format. Image of a RichTextBox is shown below.

RichTextBox

RichTextBox Event

The default event of RichtextBox is the TextChanged event which looks like this in code:

Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged

End Sub

Code Samples

Code for creating bold and italic text in a RichTextBox

Drag a RichTextBox (RichTextBox1) and a Button (Button1) onto the form. Enter some text in RichTextBox1, say, "We are working with RichTextBoxes". Paste the following code in the click event of Button1. The following code will search for text we mention in code and sets it to be displayed as Bold or Italic based on what text is searched for.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e_
As System.EventArgs) Handles Button1.Click
RichTextBox1.SelectionStart = RichTextBox1.Find("are")
'using the Find method to find the text "are" and setting it's
'return property to SelectionStart which selects the text to format
Dim ifont As New Font(RichTextBox1.Font, FontStyle.Italic)
'creating a new font object to set the font style
RichTextBox1.SelectionFont = ifont
'assigning the value selected from the RichTextBox the font style
RichTextBox1.SelectionStart = RichTextBox1.Find("working")
Dim bfont As New Font(RichTextBox1.Font, FontStyle.Bold)
RichTextBox1.SelectionFont = bfont
End Sub

When you run the above code and click Button1, the text "are" is displayed in Italic and the text "working" is displayed in Bold font. The image below displays the output.

Code for Setting the Color of Text

Lets work with previous example. Code for setting the color for particular text looks like this:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles Button1.Click
RichTextBox1.SelectionStart = RichTextBox1.Find("are")
'using the Find method to find the text "are" and setting it's return
'property to SelectionStart which selects the text 
RichTextBox1.SelectionColor = Color.Blue
'setting the color for the selected text with SelectionColor property
RichTextBox1.SelectionStart = RichTextBox1.Find("working")
RichTextBox1.SelectionColor = Color.Yellow
End Sub

The output when the Button is Clicked is the text "are" being displayed in Blue and the text "working" in yellow as shown in the image below.

Code for Saving Files to RTF

Drag two RichTextBoxes and two Buttons (Save, Load) onto the form. When you enter some text in RichTextBox1 and click on Save button, the text from RichTextBox1 is saved into a rtf (rich text format) file. When you click on Load button the text from the rtf file is displayed into RichTextBox2. The code for that looks like this:

Private Sub Save_Click(ByVal sender As System.Object, ByVal e As_
System.EventArgs) Handles Save.Click
RichTextBox1.SaveFile("hello.rtf")
'using SaveFile method to save text in a rich text box to hard disk
End Sub

Private Sub Load_Click(ByVal sender As System.Object, ByVal e As_
System.EventArgs) Handles Load.Click
RichTextBox2.LoadFile("hello.rtf")
'using LoadFile method to read the saved file
End Sub


The files which we create using the SaveFile method are saved in the bin directory of the Windows Application. You can view output of the above said code in the image above.

For Properties See: Control Object Properties - Form Properties

  Privacy Policy | Terms of Use | Site Map | Contact

  © 2004-2007 Startvbdotnet.com. All rights reserved.