Saturday, October 8, 2011

Working with Rich Text Format (RTF)

 

Rich text format allows text to have attributes such as bold, underline, colors, bold etc. It is the formatting code used for Word for Windows and many other text editing programs.If you open a file with an extension of rtf, the file will open Word for Windows or WordPad. You will not see the codes being used to render the text.Database programs, like Notepad, will strip the rtf codes and leave only the text. This gives you a way to copy a web page and paste the text only. Notepad will ignore everything but the text.

Get a trial version of this program.

Storing Rich Text Format in a database

I save the RTF contents to an rtf file and then read it back into a text field.

Contents of RTF textbox

Private Sub SaveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveButton.Click
Try
' The SaveFile save the rtf text in a coded file which is read by databases as ASCII text so it can be stored.
' Use the C:\Windows\Temp\ folder because it exist on all windows computers and allows all users to write to it.
RichTextBox1.SaveFile("C:\Windows\Temp\MyRTF.rtf")
Me.TextBox1.Text = My.Computer.FileSystem.ReadAllText("C:\Windows\Temp\MyRTF.rtf")
0' Reload the form after save
Me.DateModifiedTextBox.Text = Now
Me.Validate()
Me.TblMainBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.FreeFormDBDataSet)
Me.TblMainTableAdapter.Fill(Me.FreeFormDBDataSet.tblMain)
Me.RichTextBox1.Rtf = Me.TextBox1.Text
Catch ex As OleDb.OleDbException
MsgBox(ex.Message)
End Try
End Sub
Partial Contents of MyRTF.rtf file
{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fprq1\fcharset0 Courier New;}
{\f1\fnil\fcharset0 Arial;}}{\colortbl ;\red0\green0\blue255;\red0\green128\blue0;\red163\green21\blue21;}
\viewkind4\uc1\pard\cf1\lang1024\f0\fs20 Private\cf0 \cf1 Sub\cf0 SaveButton_Click(\cf1 ByVal\cf0

No comments:

Post a Comment