none
Tengo Problemas con guardar cambios realizados a un texto en un richtextbox (Ayuda Urgente) RRS feed

  • Pregunta

  • Hola amigos estoy desarrollando un sistema donde he incluido un editor de texto utilizando la herramienta richtextbox en visual studio 2010; a dicho editor lo he complementado con herramientas como Negrita, Subrayado, Cambio de fuente, Cambio de tamaño de fuente, cambio de color de fuente, entre otros que están en mi formulario representado con botones, también tengo cuatro botones que son muy importantes para lo que quiero que son guardar ,nuevo, abrir e imprimir el problema es que cuando mando a Guardar mis escritos lo hace pero sin los cambios que yo lo he  puesto en el momento de la ejecución del sistema es decir que si yo le centro el texto le pongo negrita le subrayo para que se vea mas presentable mi trabajo, en el momento de la ejecución lo hace, pero cuando guardo el archivo y lo vuelvo a abrir me sale el texto pero plano sin los cambios con los que yo lo guarde lo mismo me pasa cuando mando a imprimir el archivo lo imprime sin los cambios que yo le hago al texto por favor ayúdenme con esto que es importante para dar por finalizado un proyecto de mi trabajo gracias adjunto imágenes y código para su análisis...      

    Imports System.Drawing.Text
    
    Public Class Biblia
    
        Private Sub ToolStripButton7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton7.Click
    
    
            If document.Modified Then
    
    
    
                Dim ask As MsgBoxResult
    
                ask = MsgBox("¿Desea guardar cambios en el archivo actual?", MsgBoxStyle.YesNoCancel, "Nuevo Archivo")
    
                If ask = MsgBoxResult.No Then
    
                    document.Clear()
    
                ElseIf ask = MsgBoxResult.Cancel Then
    
                ElseIf ask = MsgBoxResult.Yes Then
    
                    SaveFileDialog1.ShowDialog()
    
                    My.Computer.FileSystem.WriteAllText(SaveFileDialog1.FileName, document.Text, False)
    
                    document.Clear()
    
                End If
    
    
    
            Else
    
                document.Clear()
    
            End If
        End Sub
    
        Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
    
    
            If document.Modified Then
    
    
    
                Dim ask As MsgBoxResult
    
                ask = MsgBox("¿Desea guardar cambios en el archivo actual?", MsgBoxStyle.YesNoCancel, "Abrir Archivo")
    
                If ask = MsgBoxResult.No Then
    
                    OpenFileDialog1.ShowDialog()
    
                    document.Text = My.Computer.FileSystem.ReadAllText(OpenFileDialog1.FileName)
    
                ElseIf ask = MsgBoxResult.Cancel Then
    
                ElseIf ask = MsgBoxResult.Yes Then
    
                    SaveFileDialog1.ShowDialog()
    
                    My.Computer.FileSystem.WriteAllText(SaveFileDialog1.FileName, document.Text, False)
    
                    document.Clear()
    
                End If
    
            Else
    
    
    
                OpenFileDialog1.ShowDialog()
    
                document.Text = My.Computer.FileSystem.ReadAllText(OpenFileDialog1.FileName)
    
    
    
            End If
        End Sub
    
        Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click
            SaveFileDialog1.ShowDialog()
    
    
    
            If My.Computer.FileSystem.FileExists(SaveFileDialog1.FileName) Then
    
                Dim ask As MsgBoxResult
    
                ask = MsgBox("El archivo ya existe,¿Desea reemplazarlo?", MsgBoxStyle.YesNo, "Archivos existentes")
    
    
    
                If ask = MsgBoxResult.No Then
    
                    SaveFileDialog1.ShowDialog()
    
    
    
                ElseIf ask = MsgBoxResult.Yes Then
    
                    My.Computer.FileSystem.WriteAllText(SaveFileDialog1.FileName, document.Text, False)
    
                End If
    
    
    
            Else
    
                My.Computer.FileSystem.WriteAllText(SaveFileDialog1.FileName, document.Text, False)
    
            End If
        End Sub
    
        Private Sub ToolStripButton13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton13.Click
            document.Redo()
        End Sub
    
        Private Sub ToolStripButton14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton14.Click
            document.Undo()
        End Sub
    
        Private Sub ToolStripButton18_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton18.Click
            Try
                Dim dlg As ColorDialog = New ColorDialog
                dlg.Color = document.ForeColor
                If dlg.ShowDialog = System.Windows.Forms.DialogResult.OK Then
                    document.ForeColor = dlg.Color
                End If
            Catch ex As Exception
    
            End Try
        End Sub
    
        Private Sub ToolStripButton19_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    
        End Sub
    
        Private Sub ToolStripButton4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton4.Click
            document.SelectionAlignment = 0
        End Sub
    
        Private Sub ToolStripButton5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton5.Click
            document.SelectionAlignment = 2
        End Sub
    
        Private Sub ToolStripButton6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton6.Click
            document.SelectionAlignment = 1
        End Sub
    
        Private Sub ToolStripButton8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton8.Click
            If document.SelectionBullet = False Then
                document.SelectionBullet = True
            Else : document.SelectionBullet = True
                document.SelectionBullet = False
            End If
    
    
        End Sub
    
    
        Private Sub ToolStripButton15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton15.Click
            If document.SelectionFont.Bold = False Then
                document.SelectionFont = New Font(document.SelectionFont, document.SelectionFont.Style Or FontStyle.Bold)
            Else
                document.SelectionFont = New Font(document.SelectionFont, document.SelectionFont.Style - 1)
            End If
        End Sub
    
        Private Sub ToolStripButton16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton16.Click
            If document.SelectionFont.Italic = False Then
                document.SelectionFont = New Font(document.SelectionFont, document.SelectionFont.Style Or FontStyle.Italic)
            Else
                document.SelectionFont = New Font(document.SelectionFont, document.SelectionFont.Style - 2)
            End If
        End Sub
    
        Private Sub ToolStripButton17_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton17.Click
            If document.SelectionFont.Underline = False Then
                document.SelectionFont = New Font(document.SelectionFont, document.SelectionFont.Style Or FontStyle.Underline)
            Else
                document.SelectionFont = New Font(document.SelectionFont, document.SelectionFont.Style - 4)
            End If
        End Sub
    
    
    
        Private Sub FuenteToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FuenteToolStripMenuItem.Click
            Try
                Dim dlg As FontDialog = New FontDialog
                dlg.Font = document.Font
                If dlg.ShowDialog = System.Windows.Forms.DialogResult.OK Then
                    document.Font = dlg.Font
                End If
            Catch ex As Exception : End Try
        End Sub
    
        Private Sub Biblia_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            ComboBox_fuentes.Width = 200
            Dim font_family As FontFamily
            Dim installed_fonts As New InstalledFontCollection
            ComboBox_fuentes.Items.Clear()
            For Each font_family In FontFamily.Families
                ComboBox_fuentes.Items.Add(font_family.Name)
            Next
            '************************************************
            ComboBox_fuentes.SelectedItem = "Microsoft Sans Serif"
    
        End Sub
    
        Private Sub ComboBox_fuentes_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox_fuentes.SelectedIndexChanged
            If num.Text = "- -" Then
                document.SelectionFont = New Font(New System.Drawing.FontFamily(ComboBox_fuentes.Text), 12)
            ElseIf ComboBox_fuentes.SelectedItem = ComboBox_fuentes.Text Then
                document.SelectionFont = New Font(New System.Drawing.FontFamily(ComboBox_fuentes.Text), num.Text)
            End If
            
        End Sub
    
        Private Sub num_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles num.SelectedIndexChanged
            If num.SelectedItem = num.Text Then
                document.SelectionFont = New Font(New System.Drawing.FontFamily(ComboBox_fuentes.Text), num.SelectedItem)
    
            End If
    
        End Sub
    
        Private Sub ToolStripButton9_MouseEnter(ByVal sender As System.Object, ByVal e As System.EventArgs)
    
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
            Dim sa As New SaveFileDialog
            sa.Filter = "Archivo con ext.(EXE)|*.exe|Archivo con ext.(DOC)|*.doc"
            sa.ShowDialog()
    
        End Sub
    
        Private Sub ToolStripButton20_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton20.Click
            document.SelectAll()
        End Sub
    
        Private Sub ToolStripButton21_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton21.Click
            document.Copy()
        End Sub
    
        Private Sub ToolStripButton24_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton24.Click
            Try
                Dim dlg As ColorDialog = New ColorDialog
                dlg.Color = document.BackColor
                If dlg.ShowDialog = System.Windows.Forms.DialogResult.OK Then
                    document.BackColor = dlg.Color
                End If
            Catch ex As Exception
    
            End Try
        End Sub
    
        Private Sub ToolStripButton22_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton22.Click
            document.Cut()
        End Sub
    
        Private Sub ToolStripButton23_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton23.Click
            document.Paste()
        End Sub
    
        Private Sub ToolStripButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton3.Click
    
            If PrintDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
    
                PrintDocument1.Print()
    
            End If
        End Sub
    
        Private Sub VistaPreviaDeImpresiónToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles VistaPreviaDeImpresiónToolStripMenuItem.Click
            PrintDocument1 = PrintPreviewDialog1.Document
    
            PrintPreviewDialog1.ShowDialog()
        End Sub
    
        Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
            Static currentChar As Integer
    
            Static currentLine As Integer
    
            Dim textfont As Font = document.Font
    
            Dim h, w As Integer
    
            Dim left, top As Integer
    
            With PrintDocument1.DefaultPageSettings
    
                h = .PaperSize.Height - .Margins.Top - .Margins.Bottom
    
                w = .PaperSize.Width - .Margins.Left - .Margins.Right
    
                left = PrintDocument1.DefaultPageSettings.Margins.Left
    
                top = PrintDocument1.DefaultPageSettings.Margins.Top
    
            End With
    
            e.Graphics.DrawRectangle(Pens.Blue, New Rectangle(left, top, w, h))
    
            If PrintDocument1.DefaultPageSettings.Landscape Then
    
                Dim a As Integer
    
                a = h
    
                h = w
    
                w = a
    
            End If
    
            Dim lines As Integer = CInt(Math.Round(h / textfont.Height))
    
            Dim b As New Rectangle(left, top, w, h)
    
            Dim format As StringFormat
    
            If Not document.WordWrap Then
    
                format = New StringFormat(StringFormatFlags.NoWrap)
    
                format.Trimming = StringTrimming.EllipsisWord
    
                Dim i As Integer
    
                For i = currentLine To Math.Min(currentLine + lines, document.Lines.Length - 1)
    
                    e.Graphics.DrawString(document.Lines(i), textfont, Brushes.Black, New RectangleF(left, top + textfont.Height * (i - currentLine), w, textfont.Height), format)
    
                Next
    
                currentLine += lines
    
                If currentLine >= document.Lines.Length Then
    
                    e.HasMorePages = False
    
                    currentLine = 0
    
                Else
    
                    e.HasMorePages = True
    
                End If
    
                Exit Sub
    
            End If
    
            format = New StringFormat(StringFormatFlags.LineLimit)
    
            Dim line, chars As Integer
    
            e.Graphics.MeasureString(Mid(document.Text, currentChar + 1), textfont, New SizeF(w, h), format, chars, line)
    
            If currentChar + chars < document.Text.Length Then
    
                If document.Text.Substring(currentChar + chars, 1) <> " " And document.Text.Substring(currentChar + chars, 1) <> vbLf Then
    
                    While chars > 0
    
                        document.Text.Substring(currentChar + chars, 1)
    
                        document.Text.Substring(currentChar + chars, 1)
    
                        chars -= 1
    
                    End While
    
                    chars += 1
    
                End If
    
            End If
    
            e.Graphics.DrawString(document.Text.Substring(currentChar, chars), textfont, Brushes.Black, b, format)
    
            currentChar = currentChar + chars
    
            If currentChar < document.Text.Length Then
    
                e.HasMorePages = True
    
            Else
    
                e.HasMorePages = False
    
                currentChar = 0
    
            End If
        End Sub
    
        Private Sub NuevoToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NuevoToolStripMenuItem.Click
    
    
            If document.Modified Then
    
    
    
                Dim ask As MsgBoxResult
    
                ask = MsgBox("¿Desea guardar cambios en el archivo actual?", MsgBoxStyle.YesNoCancel, "Nuevo Archivo")
    
                If ask = MsgBoxResult.No Then
    
                    document.Clear()
    
                ElseIf ask = MsgBoxResult.Cancel Then
    
                ElseIf ask = MsgBoxResult.Yes Then
    
                    SaveFileDialog1.ShowDialog()
    
                    My.Computer.FileSystem.WriteAllText(SaveFileDialog1.FileName, document.Text, False)
    
                    document.Clear()
    
                End If
    
    
    
            Else
    
                document.Clear()
    
            End If
        End Sub
    End Class
    

    gracias espero su AYUDA

    • Cambiado Enrique M. Montejo sábado, 7 de abril de 2012 17:03 Controles de Windows Forms (De:Lenguaje VB.NET)
    jueves, 22 de marzo de 2012 19:09

Todas las respuestas