Results 1 to 40 of 49

Thread: PrintPreviewcontrol

Threaded View

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    904

    PrintPreviewcontrol

    Code:
    Imports System.Data.OleDb
    
    Public Class Form1
        Dim con As OleDbConnection
    
        Dim cmd As OleDbCommand
    
        Dim da As OleDbDataAdapter
    
        Dim dt As New DataTable
    
        Private _titleFont As Font
    
        Private _detailFont As Font
    
        Private Sub PrintDocument1_BeginPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles PrintDocument1.BeginPrint
           
            _titleFont = New Font(FontFamily.GenericSerif, 16, FontStyle.Bold, GraphicsUnit.Point)
    
            _detailFont = New Font(FontFamily.GenericSerif, 12, FontStyle.Bold, GraphicsUnit.Point)
    
        End Sub
    
    
        Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
         
            Dim xPos As Single = 120
    
            Dim yPos As Single = 120
    
            Dim i As Integer = 0
    
            Dim y As Integer = 0
    
            Dim myArray() As Integer = {0, 0, 0, 0}
    
            For i = 0 To dt.Rows.Count - 1
    
                For y = 0 To dt.Columns.Count - 1
    
                    If myArray(y) < e.Graphics.MeasureString(dt.Rows(i).Item(y).ToString, _detailFont).Width + 150 Then
    
                        myArray(y) = e.Graphics.MeasureString(dt.Rows(i).Item(y).ToString, _detailFont).Width + 150
    
                    End If
    
                Next
    
            Next
    
            Dim counter As Integer = 0
    
            For Each c As DataColumn In dt.Columns
    
                e.Graphics.DrawString(c.ColumnName, _titleFont, Brushes.Black, xPos, yPos)
    
                xPos += myArray(counter)
    
                counter += 1
    
            Next
    
            xPos = 120
    
            yPos += 50
    
            For i = 0 To dt.Rows.Count - 1
    
                For y = 0 To dt.Columns.Count - 1
    
                    e.Graphics.DrawString(dt.Rows(i).Item(y).ToString, _detailFont, Brushes.Black, xPos, yPos)
    
                    xPos += myArray(y)
    
                Next
    
                xPos = 120
    
                yPos += 50
    
            Next
    
        End Sub
    
          Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
            con = New OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0;Data Source=db1.mdb")
    
            con.Open()
    
            cmd = New OleDbCommand("Select * from Table1", con)
    
            da = New OleDbDataAdapter(cmd)
    
            da.Fill(dt)
    
            PrintPreviewControl1.Document = PrintDocument1
    
        End Sub
    
        Private Sub PrintToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintToolStripButton.Click
    
            If PrintDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
    
                PrintDocument1.Print()
    
    
            End If
    
        End Sub
    
        Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
    
            PageSetupDialog1.PageSettings = PrintDocument1.DefaultPageSettings
    
            If PageSetupDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
    
                PrintDocument1.DefaultPageSettings = PageSetupDialog1.PageSettings
    
            Else
    
                Exit Sub
    
            End If
    
        End Sub
    
    End Class
    i did this code and i am having problem with the tootstripbutton1 click....

    i clicked the button,then a pagesetupdialog appears,then i changed from portrait to landscape and clicked ok but the preview of the document does not changes in the PringPreiewcontrol from landscape to Portrait........

    any ideas?
    Last edited by HowTo; Mar 27th, 2010 at 01:31 PM.

Posting Permissions

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



Click Here to Expand Forum to Full Width