dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Printing Problem

  1. #1
    Join Date
    Nov 2003
    Posts
    4

    Printing Problem

    Hi

    Please could someone point me in the right direction!

    I am using the code below to print a part of a page.
    The problem is when i type a long sentence into the text box (txt1) it continues of the printable page in one long line.

    I have set the text box property to word wrap and multiline.

    How can i get it to word wrap on the printed page ?


    e.Graphics.DrawString(txt1.Text, New Font("Arial", 10, FontStyle.Regular), _
    Brushes.Black, 100, 420)

    Regards.

  2. #2
    Join Date
    Oct 2005
    Location
    Islamabad, Pakistan
    Posts
    1,277

    Re: Printing Problem

    Sample

    Delclaration
    Code:
        Private WithEvents pdoc As New Printing.PrintDocument()
    PrintPage
    Code:
        Private Sub pdoc_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles pdoc.PrintPage
    
            Dim yPosition As Single = 0 'initial position
            Dim count As Integer = 0    'line counter
    
            Dim leftMargin As Single = e.MarginBounds.Left
            Dim topMargin As Single = e.MarginBounds.Top
    
            Dim printArea As Single = e.PageBounds.Width - e.MarginBounds.Left
            Dim x As Single = 0
            Dim xpos As Single = e.MarginBounds.Left
    
            Dim printFont As Font = Me.TextBox1.Font
            'get number of lines per page
            Dim linesPerPage As Single = e.MarginBounds.Height / printFont.GetHeight(e.Graphics)
    
            Dim words() As String = TextBox1.Text.Split(New Char() {" ", "CrLf"})
    
            Static i As Integer = 0
            While count < linesPerPage And i < words.Length
    
                ' get next line position
                yPosition = (topMargin + (count * printFont.GetHeight(e.Graphics)))
    
                Dim stringSize As New SizeF
                stringSize = e.Graphics.MeasureString(words(i), printFont)
    
                x = stringSize.Width + xpos
                If printArea > x Then
                    e.Graphics.DrawString(words(i) & " ", printFont, Brushes.Black, xpos, yPosition, New StringFormat())
    
                    xpos = x
                    i += 1
                Else
                    count += 1
    
                    'Reset x-position
                    x = 0
                    xpos = e.MarginBounds.Left
                End If
    
            End While
    
            ' If there are more lines, print another page. 
            If i < words.Length Then
                e.HasMorePages = True
            Else
                e.HasMorePages = False
            End If
    
        End Sub
    Print Button
    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim dialog As New PrintDialog()
            dialog.Document = pdoc
    
            If dialog.ShowDialog = Windows.Forms.DialogResult.OK Then
                pdoc.Print()
            End If
        End Sub

  3. #3
    Join Date
    Nov 2003
    Posts
    4

    Re: Printing Problem

    Thanks for your reply.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)