Click here to Skip to main content
14,454,989 members

Extended RichTextBox

Rate this:
3.87 (10 votes)
Please Sign up or sign in to vote.
3.87 (10 votes)
8 Nov 2008CPOL
Rich Text Box control with built in options
RichTextBoxExDemo

Introduction

We all drop and use Rich Text Box controls in our VB apps. It is a rich control (hence the name RichTextBox), with many options to format text. However, all options have to be used through coding in the background.

I have also worked with RTBs in several different applications, so I wrote this small extended control for the RichTextBox that displays a toolbar on top, with some common options that the user can perform. One cool option that I added is the spell check option (with help from another article on CodeProject, NetSpell - Spell Checker for .NET).

So instead of dropping the RichTextBox in your form, drop the Extended RichTextBox. :)

Background

I have seen several different posts for complete VB applications on the Internet that use RichTextBoxes to create WordPad type applications. They are very helpful for code reference, but you cannot just easily drop it in as a control and start using it. That is why I had to write this control.

Using the Code

Basically if you just need to use the control on your form, you can simply add the DLL as a reference, and be done with it. Download the demo project to see how that is setup. Make sure to include the en-US.dic file in your project. You can still use the control just as you would use a RichTextBox. The only difference is that it shows a nifty toolbox on top with options.

The actual control project is also included, so you can easily add other options and buttons if you need, or maybe even remove one if needed. All you have to do is handle the button click.

For example, this is how I handle toggling bullets:

Private Sub BulletsToolStripButton_Click(ByVal sender As System.Object, _
	ByVal e As System.EventArgs) Handles BulletsToolStripButton.Click
	rtb.SelectionBullet = Not rtb.SelectionBullet
	BulletsToolStripButton.Checked = rtb.SelectionBullet
End Sub

The first line performs the function, and the second line toggles the button for the function. Most function implementations are just that simple.

Points of Interest

Note that if you select text that has formatting, it actually toggles the boxes. This is handled in the SelectionChanged event of the RichTextBox by simply setting the buttons checked value by comparing what the settings are at that position in the RichTextBox.

Possible Future Updates

I might post an update with options to hide/show each button, so that it makes it easy for the control user to control it better. Also, there are tons of features to add, like ComboBox font and font size selection, picture/object insert, save/open RTF files, etc.

History

  • 8th November, 2008: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Razi Syed
Software Developer (Senior)
United States United States
My area of expertise is developing management database systems for business so that they can efficiently store and lookup existing customer's information and related data, and be able to generate various reports. My goal is to always deliver innovative design and a user friendly interface.

Comments and Discussions

 
GeneralRe: How to access the text property or equivalent Pin
Member 105712894-Feb-14 9:52
MemberMember 105712894-Feb-14 9:52 
GeneralRe: How to access the text property or equivalent Pin
Member 1060025516-Feb-14 23:29
MemberMember 1060025516-Feb-14 23:29 
GeneralRe: How to access the text property or equivalent Pin
Member 822511426-Oct-14 10:39
MemberMember 822511426-Oct-14 10:39 
GeneralRe: How to access the text property or equivalent Pin
Razi Syed12-Mar-14 5:09
MemberRazi Syed12-Mar-14 5:09 
AnswerRe: How to access the text property or equivalent Pin
Helio Cabral24-Oct-17 5:48
MemberHelio Cabral24-Oct-17 5:48 
GeneralRe: How to access the text property or equivalent Pin
Razi Syed3-Nov-17 9:59
MemberRazi Syed3-Nov-17 9:59 
QuestionIs there support for adding pictures ? Pin
Masoud Dehghani10-Nov-08 10:03
MemberMasoud Dehghani10-Nov-08 10:03 
AnswerRe: Is there support for adding pictures ? Pin
Razi Syed10-Nov-08 12:06
MemberRazi Syed10-Nov-08 12:06 
I haven't added support for pictures yet, but it is fairly easy to add.

Add a button for inserting picture, and then write code to display a dialog to open an image, copy that image in to the clipboard, and paste it in the RichTextBox.

Here is the code:

Private Sub ImageToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ImageToolStripButton.Click
' Show dialog to load Image
OpenFileDlg.Title = "Select Image File"
OpenFileDlg.Filter = "Bitmap Files|*.bmp|JPEG Files|*.jpg|GIF Files|*.gif"
OpenFileDlg.FilterIndex = 1
If OpenFileDlg.ShowDialog() = Windows.Forms.DialogResult.Cancel Then Exit Sub

Try
Dim img As Image

' load Image in picture box
img = Image.FromFile(OpenFileDlg.FileName)

' copy picture to the clipboard
Clipboard.SetDataObject(img)

' and paste it in to the rich textbox
rtb.Paste()

Catch ex As Exception
MsgBox("Unable to insert image format selected.", MsgBoxStyle.Exclamation, "Error")
End Try

End Sub

Hope it helps. I'll post a new one with this code when I have the chance.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Article
Posted 8 Nov 2008

Stats

115.8K views
14.4K downloads
43 bookmarked