Reading rtf files

Hi again, right my problem is I have made a form of writing program that can have writing bold, italics, different font etc..

My problem is when is when I read a document from my applications rtftextbox it will only show the text of the file and no form of formatting but if I save a file with bold formatting and I open the document in notepad then the document will show with the formatting. So I think there is something wrong with my read function and I was hoping someone could help me as I'd be most greatful. Example code that would do me is if I wanted to read a document(.txt or .rtf dont mind) that had bold formatting, my read/open file function is below:

Public Sub metOpen() 'opens a file

Dim intFileNameLength As Integer

Dim strCurrentFileName As String

If frmMain.Text.Contains("*") = True Then

Dim Message As String = "You have not recently saved the current form, do you wish to save "

Dim Caption As String = "OSSType"

Dim Buttons As MessageBoxButtons = MessageBoxButtons.YesNo

Dim Result As DialogResult

Result = MessageBox.Show(Message, Caption, Buttons)

If Result = Windows.Forms.DialogResult.Yes Then

Dim savedocument As New clsMenu 'new instance of clsMenu

savedocument.metSaveAs()

End If

Dim Read As New OpenFileDialog()

intFileNameLength = frmMain.Text.Length

strCurrentFileName = frmMain.Text.Remove(intFileNameLength - 1, 1)

With Read

.Filter = "All Files (*.*)|*.*|OSSType files (*.osstype)|*.osstype|Text Files (*.txt)|*.txt|Rich format files (*.rtf)|*.rtf"

.CheckPathExists = True

.Title = "Open file:"

.FileName = strCurrentFileName

If .ShowDialog() = DialogResult.OK Then

Try

Dim EntireFile As String

oRead = IO.File.OpenText(.FileName)

EntireFile = oRead.ReadToEnd() 'EntireFile reads all the data in the file selected in openDialog

frmMain.rtfMainText.Text = EntireFile

frmMain.Text = .FileName

Catch ex As Exception

' Do nothing on Exception

End Try

End If

End With

Try

oWrite.Close()

Catch ex As Exception 'if user clicks cancel in save file box

End Try

Try

oRead.Close()

Catch ex As Exception 'if user clicks cancel in open file box

End Try

ElseIf frmMain.Text.Contains("*") = False Then

Dim Read As New OpenFileDialog()

With Read

.Filter = "All Files (*.*)|*.*|OSSType files (*.osstype)|*.osstype|Text Files (*.txt)|*.txt|Rich format files (*.rtf)|*.rtf"

.CheckPathExists = True

.Title = "Open file:"

.FileName = frmMain.Text

If .ShowDialog() = DialogResult.OK Then

Try

Dim EntireFile As String

oRead = IO.File.OpenText(.FileName)

EntireFile = oRead.ReadToEnd() 'EntireFile reads all the data in the file selected in openDialog

frmMain.rtfMainText.Text = EntireFile

frmMain.Text = .FileName

Catch ex As Exception

' Do nothing on Exception

End Try

End If

oRead.Close()

End With

End If

end sub

Hope someone can help!



Answer this question

Reading rtf files

  • wichall

    rtfMainText = A rich Text format box on my main form (frmMain) with your advise I dabbled for a few minutes and now if I save in my own format (.osstype) or (.rtf) then everything works how it should do.
  • Vicious1

    You have not told me what is the rtfMainText control

    If it is a RichTextBox, then you should use SaveFile to save the text.

    Otherwise, you should find out what the control can do for you.

    I would be supprise that frmMain.rtfMainText.Text can give you the formated text in a file.


  • Ross B.

    Is the rtftextbox implemented by you How do you save the text to the file

    RichTextBox has a pair of methods called SaveFile and LoadFile.

    Your rtftextbox should implement something simlilar.

    The way you are opening the file

              oRead = IO.File.OpenText(.FileName)

              EntireFile = oRead.ReadToEnd()

    will read the file as a text string and I guess the font and format is encoded as text string as well and you should parse the string and display it appropriately.


  • Boris Resnick

    thanks for your replies guys, this is my write:

    Public Sub metSaveAs() 'iniciates action to save as

    Dim Save As New SaveFileDialog()

    Dim intFileNameLength As Integer

    Dim strCurrentFileName As String = frmMain.Text

    If frmMain.Text.Contains("*") = True Then

    intFileNameLength = frmMain.Text.Length

    strCurrentFileName = frmMain.Text.Remove(intFileNameLength - 1, 1)

    End If

    With Save 'contruction of save as dialog

    .Filter = "All Files (*.*)|*.*|OSSType files (*.osstype)|*.osstype|Text Files (*.txt)|*.txt|Rich format files (*.rtf)|*.rtf"

    .CheckPathExists = True

    .Title = "Save as: "

    .FileName = strCurrentFileName

    If .ShowDialog() = DialogResult.OK Then

    Try

    oWrite = System.IO.File.AppendText(.FileName) 'oWrite holds the value of writing to filename selected at savedialog time

    oWrite.Write(frmMain.rtfMainText.Text)'gets all the data from txtMainText.text and writes it to whatever file name was stored in oWrite above

    frmMain.Text = .FileName

    Catch ex As Exception

    ' Do nothing on Exception

    End Try

    oWrite.Close()

    End If

    End With

    End Sub


  • blr

    Instead of setting the Text property of the rtb, try using the LoadFile method of the rtb to read in the rtf file directly. The Text property is JUST the text without any markup.
  • RobMc

    You both answered my question you lot on here just know way to much! thanks again!
  • Reading rtf files