RTF Problems

hi everybody i have just 2Quick questions to ask:

3)when I open any rich text document in my rich text box the formatting is lost and all I get is a ton of font code

4)in my rich text box if I highlight any text my application crashes, in VB, before I compile it I get a warning saying that stream reader and stream writer are being used in code before they are declared, but looking at the code that looks OK .

My Code:

Imports Microsoft.VisualBasic.FileIO

Public Class MainWindow

Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click

Dim sr As IO.StreamReader

'streamreader is used to read text

Dim FileName As String = String.Empty

'declaring filename that will be selected

Try

With OpenFileDialog1

'With statement is used to execute statements using a particular object, here,_

'OpenFileDialog1

.Filter = "Text files (*.txt)|*.txt|" & "Rich Text Documents (*.rtf)|*.rtf|" & "All files|*.*"

'setting filters so that Text files and All Files choice appears in the Files of Type box

'in the dialog

If .ShowDialog() = Windows.Forms.DialogResult.OK Then

'showDialog method makes the dialog box visible at run time

FileName = .FileName

sr = New IO.StreamReader(.OpenFile)

'using streamreader to read the opened text file

RichTextBox1.Text = sr.ReadToEnd()

'displaying text from streamreader in richtextbox

End If

End With

Catch es As Exception

MessageBox.Show(es.Message)

Finally

If Not (sr Is Nothing) Then

sr.Close()

End If

End Try

End Sub

Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click

Dim sw As IO.StreamWriter

'streamwriter is used to write text

Dim FileName As String = String.Empty

'declaring filename that will be selected

Try

With SaveFileDialog1

.FileName = FileName

.Filter = "Text files (*.txt)|*.txt|" & "Rich Text Files(*.rtf)|*.rtf|" & "All files|*.*"

If .ShowDialog() = Windows.Forms.DialogResult.OK Then

FileName = .FileName

sw = New IO.StreamWriter(FileName)

'using streamwriter to write text from richtextbox and saving it

sw.Write(RichTextBox1.Text)

End If

End With

Catch es As Exception

MessageBox.Show(es.Message)

Finally

If Not (sw Is Nothing) Then

sw.Close()

End If

End Try

End Sub

Private Sub PrintToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

End Sub

Private Sub PrintPreviewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

End Sub

Private Sub FontCtrlFToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FontCtrlFToolStripMenuItem.Click

Try

With FontDialog1

.Font = RichTextBox1.Font

'initializing the dialog box to match the font used in the richtextbox

.Color = RichTextBox1.ForeColor

'default color is Black

If .ShowDialog = Windows.Forms.DialogResult.OK Then

setFont()

'calling a method setFont() to set the selected font and color

End If

End With

Catch es As Exception

MessageBox.Show(es.Message)

End Try

End Sub

Private Sub setFont()

Try

With FontDialog1

RichTextBox1.Font = .Font

If .ShowColor Then

RichTextBox1.ForeColor = .Color

'setting the color

End If

End With

Catch ex As Exception

MessageBox.Show(ex.Message)

End Try

End Sub

Private Sub AboutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutToolStripMenuItem.Click

AboutBox1.ShowDialog()

End Sub

Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk

End Sub

Private Sub NewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripMenuItem.Click

RichTextBox1.Text = " "

'clears the text in richtextbox

End Sub

Private Sub FontDialog1_Apply(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FontDialog1.Apply

End Sub

Private Sub SaveFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles SaveFileDialog1.FileOk

End Sub

Private Sub ImportText(ByVal filename As String)

End Sub

Private Sub ExportText(ByVal filename As String)

End Sub

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

End Sub

Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click

Me.Close()

End Sub

Public NotInheritable Class PrintDialog

Dim CommonDialog

End Class

Private Sub PrintToolStripMenuItem1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintToolStripMenuItem1.Click

' Declare the PrintDocument object.

Dim docToPrint As New Printing.PrintDocument

' This method will set properties on the PrintDialog object and

' then display the dialog.

' Allow the user to choose the page range he or she would

' like to print.

PrintDialog1.AllowSomePages = True

' Show the help button.

PrintDialog1.ShowHelp = True

' Set the Document property to the PrintDocument for

' which the PrintPage Event has been handled. To display the

' dialog, either this property or the PrinterSettings property

' must be set

PrintDialog1.Document = docToPrint

Dim result As DialogResult = PrintDialog1.ShowDialog()

' If the result is OK then print the document.

If (result = Windows.Forms.DialogResult.OK) Then

docToPrint.Print()

End If

' The PrintDialog will print the document

' by handling the document's PrintPage event.

End Sub

Dim docToPrint As New Printing.PrintDocument

Private Sub document_PrintPage(ByVal sender As Object, _

ByVal e As System.Drawing.Printing.PrintPageEventArgs)

Dim docToPrint As New Printing.PrintDocument

' Insert code to render the page here.

' This code will be called when the control is drawn.

' The following code will render a simple

' message on the printed document.

Dim text As String = "In document_PrintPage method."

Dim printFont As New System.Drawing.Font _

("Arial", 35, System.Drawing.FontStyle.Regular)

' Draw the content.

e.Graphics.DrawString(text, printFont, _

System.Drawing.Brushes.Black, 10, 10)

End Sub

Private Sub CopyToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CopyToolStripMenuItem.Click

Clipboard.SetText(Me.RichTextBox1.SelectedText)

End Sub

Private Sub PasteToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PasteToolStripMenuItem.Click

Me.RichTextBox1.Paste()

End Sub

Private Sub CutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CutToolStripMenuItem.Click

Me.RichTextBox1.Cut()

End Sub

End Class




Answer this question

RTF Problems

  • HexOffender

    OK i'm trying to publish it to test it (theres something wrong with this project and it doesnt load right).

    OK it worked with stream reader/writer for rtf&txt after i fixed a little glitch, but i changed it to your way thinking it woud work better and now it doesn't open any type of file (.txt or .rtf)!!! So i think i'll stick with stream reader/writer.

    It still crashes when highlighting text, here's the errer message i get:

    Unhandled exeption has occurred in you application. Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

    Details:

    See the end of this message for details on invoking
    just-in-time (JIT) debugging instead of this dialog box.

    ************** Exception Text **************
    System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
    at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
    at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
    at System.Windows.Forms.Control.DefWndProc(Message& m)
    at System.Windows.Forms.Control.WndProc(Message& m)
    at System.Windows.Forms.TextBoxBase.WndProc(Message& m)
    at System.Windows.Forms.RichTextBox.WndProc(Message& m)
    at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
    at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
    at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


    ************** Loaded Assemblies **************
    mscorlib
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.42 (RTM.050727-4200)
    CodeBase: file:///C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
    ----------------------------------------
    Jotter 1.0
    Assembly Version: 1.0.0.0
    Win32 Version: 1.0.0.0
    CodeBase: file:///H:/SPPrograms/Programs/Ver%201.0.0.2/Jotter%201.0_1_0_0_2/Jotter%201.0.exe
    ----------------------------------------
    Microsoft.VisualBasic
    Assembly Version: 8.0.0.0
    Win32 Version: 8.0.50727.42 (RTM.050727-4200)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/Microsoft.VisualBasic/8.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualBasic.dll
    ----------------------------------------
    System
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.42 (RTM.050727-4200)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
    ----------------------------------------
    System.Windows.Forms
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.42 (RTM.050727-4200)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
    ----------------------------------------
    System.Drawing
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.42 (RTM.050727-4200)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
    ----------------------------------------
    System.Runtime.Remoting
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.42 (RTM.050727-4200)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Runtime.Remoting/2.0.0.0__b77a5c561934e089/System.Runtime.Remoting.dll
    ----------------------------------------
    skincrafter.net-vs2005_light
    Assembly Version: 2.1.0.0
    Win32 Version: 2, 1, 0, 0
    CodeBase: file:///H:/SPPrograms/Programs/Ver%201.0.0.2/Jotter%201.0_1_0_0_2/skincrafter.net-vs2005_light.DLL
    ----------------------------------------
    msvcm80
    Assembly Version: 8.0.50727.163
    Win32 Version: 8.00.50727.163
    CodeBase: file:///C:/WINDOWS/WinSxS/x86_Microsoft.VC80.CRT_1fc8b3b9a1e18e3b_8.0.50727.163_x-ww_681e29fb/msvcm80.dll
    ----------------------------------------
    Accessibility
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.42 (RTM.050727-4200)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/Accessibility/2.0.0.0__b03f5f7f11d50a3a/Accessibility.dll
    ----------------------------------------

    ************** JIT Debugging **************
    To enable just-in-time (JIT) debugging, the .config file for this
    application or computer (machine.config) must have the
    jitDebugging value set in the system.windows.forms section.
    The application must also be compiled with debugging
    enabled.

    For example:

    <configuration>
    <system.windows.forms jitDebugging="true" />
    </configuration>

    When JIT debugging is enabled, any unhandled exception
    will be sent to the JIT debugger registered on the computer
    rather than be handled by this dialog box.

    I dont think my memory is corrupt, because not a single other program crashed for something as trivial as higlighting text!



  • rocky050371

    Same with saving the file:

    Me.RichTextBox1.SaveFile("MYFILE", RichTextBoxStreamType.RichText)



  • Bolugbe

    Anyone

  • LpAngelRob

    RichTextBox1.Text = sr.ReadToEnd()

    Should be RichTextbox1.RTF = sr.readtoend()

    The file that you are importing must be a properly formatted RTF file...in addition to that the richtextbox has its own method for loading files:

    Me.RichTextBox1.LoadFile("C:\MyRTF.rtf", RichTextBoxStreamType.RichText)



  • BadFog

      Great, files open grat now (RichTextbox1.RTF = sr.readtoend()) but im not sure how to use    Me.RichTextBox1.LoadFile("C:\MyRTF.rtf", RichTextBoxStreamType.RichText)  Where does it go

    Also the program still crashes when i try to highlight text



  • Back2Escape

    Get rid of the streamreader completely...it is not needed...

    With OpenFileDialog1

    .Filter = "Text files (*.txt)|*.txt|" & "Rich Text Documents (*.rtf)|*.rtf"

    If .ShowDialog() = Windows.Forms.DialogResult.OK Then

    Dim MyFileInfo As New IO.FileInfo(.FileName)

    Select Case MyFileInfo.Extension.ToLower

    Case Is = "rtf"

    Me.RichTextBox1.LoadFile(.FileName, RichTextBoxStreamType.RichText)

    Case Is = "txt"

    Me.RichTextBox1.LoadFile(.FileName, RichTextBoxStreamType.PlainText)

    End Select

    End If

    End With

    Since you are allowing your users to select text files also you will have to test for the file type before actually loading it...



  • RTF Problems