RichTextBox printen

Status
Niet open voor verdere reacties.

Tariq

Gebruiker
Lid geworden
27 okt 2008
Berichten
109
Goedemiddag,

Zo te zien stel ik aardig wat vragen :eek:

Ik zit nu met de vraag:
Hoe print ik een RichTextBox uit met behulp van een button en een PrintForm?

In ieder geval bedankt!

Tariq

P.s.: Dit zou een oplossing moeten zijn, maar deze site laat mijn hoofd duizelen: http://support.microsoft.com/kb/811401
 
mischien Heb je wat aan deze code

Code:
Imports System.Drawing.Printing

Public Class MainForm

    Private Sub btnPrintDialog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrintDialog.Click
        PrintDialog1.Document = PrintDocument1
        If PrintDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            PrintDocument1.Print()
        End If
    End Sub

   
    Private Sub btnPrintPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrintPreview.Click
        Try
            PrintPreviewDialog1.Document = PrintDocument1
            PrintPreviewDialog1.ShowDialog()
        Catch exp As Exception
            MsgBox("An error occurred while trying to load the " & _
                "document for Print Preview. Make sure you currently have " & _
                "access to a printer. A printer must be connected and " & _
                "accessible for Print Preview to work.", MsgBoxStyle.OkOnly, _
                 Me.Text)
        End Try
    End Sub

   
    Private Sub btnPageSetup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPageSetup.Click
        With PageSetupDialog1
            .Document = PrintDocument1
            .PageSettings = PrintDocument1.DefaultPageSettings
        End With

        If PageSetupDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            PrintDocument1.DefaultPageSettings = PageSetupDialog1.PageSettings
        End If
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        txtDocument.Text = My.Resources.Address
    End Sub

   
    Private Sub pdoc_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
      
        Static intCurrentChar As Int32
        ' Initialize the font to be used for printing.
        Dim font As New Font("Microsoft Sans Serif", 24)

        Dim intPrintAreaHeight, intPrintAreaWidth, marginLeft, marginTop As Int32
        With PrintDocument1.DefaultPageSettings
            
            intPrintAreaHeight = .PaperSize.Height - .Margins.Top - .Margins.Bottom
            intPrintAreaWidth = .PaperSize.Width - .Margins.Left - .Margins.Right

            
            marginLeft = .Margins.Left ' X coordinate
            marginTop = .Margins.Top ' Y coordinate
        End With

        ' If the user selected Landscape mode, swap the printing area height 
        ' and width.
        If PrintDocument1.DefaultPageSettings.Landscape Then
            Dim intTemp As Int32
            intTemp = intPrintAreaHeight
            intPrintAreaHeight = intPrintAreaWidth
            intPrintAreaWidth = intTemp
        End If

     
        Dim intLineCount As Int32 = CInt(intPrintAreaHeight / font.Height)

        Dim rectPrintingArea As New RectangleF(marginLeft, marginTop, intPrintAreaWidth, intPrintAreaHeight)

        Dim fmt As New StringFormat(StringFormatFlags.LineLimit)
      
        Dim intLinesFilled, intCharsFitted As Int32
        e.Graphics.MeasureString(Mid(txtDocument.Text, intCurrentChar + 1), font, _
                    New SizeF(intPrintAreaWidth, intPrintAreaHeight), fmt, _
                    intCharsFitted, intLinesFilled)

        ' Print the text to the page.
        e.Graphics.DrawString(Mid(txtDocument.Text, intCurrentChar + 1), font, _
            Brushes.Black, rectPrintingArea, fmt)

      
        intCurrentChar += intCharsFitted

      
        If intCurrentChar < txtDocument.Text.Length Then
            e.HasMorePages = True
        Else
            e.HasMorePages = False

            intCurrentChar = 0
        End If
    End Sub

    Private Sub exitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitToolStripMenuItem.Click
        Me.Close()
    End Sub
End Class
 
Sorry voor de volgende opmerking:

Moet het voor het printen echt zo'n lange tekst zijn?

Ik bedoel: Voor openen en opslaan heb je ook maar minimaal 4 regeltjes nodig, waarom voor printen dan zoveel?

Bij openen en opslaan is het:

Code:
    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
        If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            RichTextBox1.SaveFile(SaveFileDialog1.FileName)
        End If
    End Sub

waarom is het bij printen dan niet:
Code:
 Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
If PrintDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
RichTextBox1.PrintFile(PrintFileDialog1.File)
End If
End Sub

Ik hoor graag als het wat simpeler kan :o
 
Het is maar wat je wild je kan gedeelte van de code gebruiken
met deze code kan je een printer kiezen een printPrieview en een print dialogen een print page maken of een uitbreiden. volens het zelfde principe je kan dus kiezen wat je wild gebruiken.
 
Me de code verwerken gaat het steeds een stukje beter, helaas ziet Visual Studio e.graphic en e.HasMorePages niet als een correcte code. Wat moet ik daaraan doen?

[Edit] Gek genoeg doet hij het gewoon ondanks dat er wordt gezegd dat het kapot is. Duizendmaal dank![/edit]


Helaas, er komt alleen een wit blaadje uit de printer rollen :-(

Wat ben ik vergeten? Ik heb gebruik gemaakt van code:

Code:
    Private Sub pdoc_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
      
        Static intCurrentChar As Int32
        ' Initialize the font to be used for printing.
        Dim font As New Font("Microsoft Sans Serif", 24)

        Dim intPrintAreaHeight, intPrintAreaWidth, marginLeft, marginTop As Int32
        With PrintDocument1.DefaultPageSettings
            
            intPrintAreaHeight = .PaperSize.Height - .Margins.Top - .Margins.Bottom
            intPrintAreaWidth = .PaperSize.Width - .Margins.Left - .Margins.Right

            
            marginLeft = .Margins.Left ' X coordinate
            marginTop = .Margins.Top ' Y coordinate
        End With

        ' If the user selected Landscape mode, swap the printing area height 
        ' and width.
        If PrintDocument1.DefaultPageSettings.Landscape Then
            Dim intTemp As Int32
            intTemp = intPrintAreaHeight
            intPrintAreaHeight = intPrintAreaWidth
            intPrintAreaWidth = intTemp
        End If

     
        Dim intLineCount As Int32 = CInt(intPrintAreaHeight / font.Height)

        Dim rectPrintingArea As New RectangleF(marginLeft, marginTop, intPrintAreaWidth, intPrintAreaHeight)

        Dim fmt As New StringFormat(StringFormatFlags.LineLimit)
      
        Dim intLinesFilled, intCharsFitted As Int32
        e.Graphics.MeasureString(Mid(txtDocument.Text, intCurrentChar + 1), font, _
                    New SizeF(intPrintAreaWidth, intPrintAreaHeight), fmt, _
                    intCharsFitted, intLinesFilled)

        ' Print the text to the page.
        e.Graphics.DrawString(Mid(txtDocument.Text, intCurrentChar + 1), font, _
            Brushes.Black, rectPrintingArea, fmt)

      
        intCurrentChar += intCharsFitted

      
        If intCurrentChar < txtDocument.Text.Length Then
            e.HasMorePages = True
        Else
            e.HasMorePages = False

            intCurrentChar = 0
        End If
    End Sub

Over E.graphics en e.HasMorePages : In een leeg project reageren die wel 'normaal'
 
Laatst bewerkt:
Laat mijn vorige bericht maar, de oplossing daarvan is al gevonden:

Code:
Private Sub pdoc_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
        Static intCurrentChar As Int32
        Dim FT = RichTextBox1.Font.Name
        Dim FS = RichTextBox1.Font.Size
        Dim font As New Font(FT, FS)
        Dim intPrintAreaHeight, intPrintAreaWidth, marginLeft, marginTop As Int32
        With PrintDocument1.DefaultPageSettings
            intPrintAreaHeight = .PaperSize.Height - .Margins.Top - .Margins.Bottom
            intPrintAreaWidth = .PaperSize.Width - .Margins.Left - .Margins.Right
            marginLeft = .Margins.Left
            marginTop = .Margins.Top
        End With
        Dim intLineCount As Int32 = CInt(intPrintAreaHeight / font.Height)
        Dim rectPrintingArea As New RectangleF(marginLeft, marginTop, intPrintAreaWidth, intPrintAreaHeight)
        Dim fmt As New StringFormat(StringFormatFlags.LineLimit)

        Dim intLinesFilled, intCharsFitted As Int32
        e.Graphics.MeasureString(Mid(RichTextBox1.Text, intCurrentChar + 1), font, _
                    New SizeF(intPrintAreaWidth, intPrintAreaHeight), fmt, _
                    intCharsFitted, intLinesFilled)
        e.Graphics.DrawString(Mid(RichTextBox1.Text, intCurrentChar + 1), font, _
            Brushes.Black, rectPrintingArea, fmt)
        intCurrentChar += intCharsFitted
        If intCurrentChar < RichTextBox1.Text.Length Then
            e.HasMorePages = True
        Else
            e.HasMorePages = False
            intCurrentChar = 0
        End If

(Moest de eerste regel een beetje aanpassen.)

Nu zit ik nog met het probleem: Hoe maak ik een knopje hiernaartoe en hoe print het het juiste document? (er komt namelijk een witte pagina uitrollen, i.p.v. eentje met tekst) De printer treed wel in werking.

Alvast bedankt!
 
Ondertussen is de volgende error gekomen, met de code van de eerste oplossing:

Kan een object van het type System.Windows.Forms.MouseEventArgs niet converteren naar het type System.Drawing.Printing.PrintPageEventArgs.

Code:
 Private Sub Button10_Click(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles Button10.Click
        Static intCurrentChar As Int32
        Dim FT = RichTextBox1.Font.Name
        Dim FS = RichTextBox1.Font.Size
        Dim font As New Font(FT, FS)
        Dim intPrintAreaHeight, intPrintAreaWidth, marginLeft, marginTop As Int32
        With PrintDocument1.DefaultPageSettings
            intPrintAreaHeight = .PaperSize.Height - .Margins.Top - .Margins.Bottom
            intPrintAreaWidth = .PaperSize.Width - .Margins.Left - .Margins.Right
            marginLeft = .Margins.Left
            marginTop = .Margins.Top
        End With
        Dim intLineCount As Int32 = CInt(intPrintAreaHeight / font.Height)
        Dim rectPrintingArea As New RectangleF(marginLeft, marginTop, intPrintAreaWidth, intPrintAreaHeight)
        Dim fmt As New StringFormat(StringFormatFlags.LineLimit)
        Dim intLinesFilled, intCharsFitted As Int32
        e.Graphics.MeasureString(Mid(RichTextBox1.Text, intCurrentChar + 1), font, _
                    New SizeF(intPrintAreaWidth, intPrintAreaHeight), fmt, _
                    intCharsFitted, intLinesFilled)
        e.Graphics.DrawString(Mid(RichTextBox1.Text, intCurrentChar + 1), font, _
            Brushes.Black, rectPrintingArea, fmt)
        intCurrentChar += intCharsFitted
        If intCurrentChar < RichTextBox1.Text.Length Then
            e.HasMorePages = True
        Else
            e.HasMorePages = False
            intCurrentChar = 0
        End If
    End Sub

Ik ga nu kijken naar het .zip bestand dat is gestuurd.
 
Het zip-bestand gaf de oplossing!

Ik heb mijn project weliswaar gedeeltelijk veranderd dan wat ik in mijn hoofd had, het is nu veel beter!

Vraag is opgelost.

Duizendenmalen dank! Old Hippy
 
OK Tariq
Blij je te hebben kunnen helpen

Was je probleem met de data verbinding ook opgelost ???
zo ja ben ik geintreseerd in je oplossing.
Daar ik met het zelfde probleem zit
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan