Solved

PrintPreview -> Print dialog -> Printer

Posted on 2004-04-20
5
2,375 Views
Last Modified: 2008-01-16
I am using a printpreview dialog to preview a print job in my application.  Right now, I show the print dialog from my form, and the settings selected on that go to the print preview and clicking on the printer button on the preview prints out the data as shown.  

What I would like to do is that when the user clicks the print preview on the form, it goes right to the print preview.  Clicking the printer icon button, I display a standard print dialog which would send the job to the printer when the user clicks OK.

Is it possible?  Can anyone give a simple sample or some basic steps?
0
Comment
Question by:PaulHews
  • 2
  • 2
5 Comments
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 10869554
You can use the printpreviewcontrol instead of the printpreviewdialog control, in the MSDN help there is a nice example for this.

0
 
LVL 25

Accepted Solution

by:
RonaldBiemans earned 250 total points
ID: 10869566
0
 
LVL 38

Author Comment

by:PaulHews
ID: 10870714
That's useful, thanks.  It's going to take a bit of work to "redo" the print preview dialog, but I guess it's worth it.  I'll accept that answer and probably post some sample code when I'm done.

Thanks.
0
 
LVL 38

Author Comment

by:PaulHews
ID: 10872287
Although much of this relates to my specific project, maybe someone somewhere will find some of it useful:

Public Class frmPreview
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "
<snipped>

    Private mTableFormat As TableFormat
    Private mFormSel As FormSelection
    Private mPrintChart As Boolean
    Private mCurrPage As Int32
    Private mCurrChartPage As Int32
   
    Private Sub frmPreview_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        With cboZoom
            .Items.Add("Auto")
            .Items.Add("200%")
            .Items.Add("150%")
            .Items.Add("100%")
            .Items.Add("75%")
            .Items.Add("50%")
            .SelectedIndex = 0
        End With

        With cboPageDisplay
            .Items.Add("1 Page")
            .Items.Add("2 Pages")
            .Items.Add("3 Pages")
            .Items.Add("4 Pages")
            .Items.Add("6 Pages")
            .SelectedIndex = 0
        End With
        NumUpDownPages.Value = 1
    End Sub

    Private Sub cboZoom_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboZoom.SelectedIndexChanged
        Dim ZoomValue As Double = 1
        Dim IsAuto As Boolean = False

        Select Case cboZoom.SelectedIndex
            Case 0
                IsAuto = True
            Case 1
                ZoomValue = 2
            Case 2
                ZoomValue = 1.5
            Case 3
                ZoomValue = 1
            Case 4
                ZoomValue = 0.75
            Case 5
                ZoomValue = 0.5
        End Select
        With PrintPreviewControl1
            .AutoZoom = IsAuto
            If Not IsAuto Then
                .Zoom = ZoomValue
            End If
            .Refresh()
        End With
    End Sub

    Private Sub cboPageDisplay_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboPageDisplay.SelectedIndexChanged
        Dim Rows, Cols As Int32

        Select Case cboPageDisplay.SelectedIndex
            Case 0
                Rows = 1
                Cols = 1
            Case 1
                Rows = 1
                Cols = 2
            Case 2
                Rows = 1
                Cols = 3
            Case 3
                Rows = 2
                Cols = 2
            Case 4
                Rows = 2
                Cols = 3
        End Select
        With PrintPreviewControl1
            .Rows = Rows
            .Columns = Cols
            .Refresh()
        End With
        Dim Max As Int32 = MaxPageStart()
        If NumUpDownPages.Value > Max Then
            NumUpDownPages.Value = Max
        End If
    End Sub

    Private Sub NumUpDownPages_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles NumUpDownPages.ValueChanged
        Dim Max As Int32
        Max = MaxPageStart()
        With NumUpDownPages
            If .Value < 1 Then .Value = 1
            If .Value > Max Then .Value = Max
            PrintPreviewControl1.StartPage = CInt(.Value) - 1
            PrintPreviewControl1.Refresh()
        End With

    End Sub
    Private Function MaxPageStart() As Int32
        Dim Max As Int32
        Dim PageDisplay As Int32 = PrintPreviewControl1.Rows * PrintPreviewControl1.Columns

        If mPrintChart Then
            Max = mTableFormat.NumChartPages - PageDisplay + 2
        Else
            Max = mTableFormat.NumPages - PageDisplay + 2
        End If
        If Max < 1 Then Max = 1

        Return Max
    End Function
    Public Sub New(ByVal TableFormat As TableFormat, ByVal FormSel As FormSelection, ByVal PrintChart As Boolean)

        MyBase.New()
        InitializeComponent()

        Try
            mTableFormat = TableFormat
            mPrintChart = PrintChart
            mFormSel = FormSel

            If PrintChart Then
                lblMaxPages.Text = String.Format("of {0}", mTableFormat.NumChartPages + 1)
                PreviewPrintChart()
            Else
                lblMaxPages.Text = String.Format("of {0}", mTableFormat.NumPages + 1)
                PreviewPrintGrid()
            End If
        Catch ex As Exception
            Throw ex
        End Try

    End Sub
    Private Sub PreviewPrintChart()
        Try
            Dim FromPg, ToPg As Int32
            Dim Copies As Int32 = 1
            Dim Collate As Boolean = False

            FromPg = 1

            ToPg = mTableFormat.NumPages + 1
            Dim PrintObj As PrintHandler = New PrintHandler(mTableFormat, mFormSel, FromPg, ToPg, Copies, Collate)
            PrintObj.PrintPreviewChart(PrintPreviewControl1)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Private Sub PreviewPrintGrid()
        Try
            Dim FromPg, ToPg As Int32
            Dim Copies As Int32 = 1
            Dim Collate As Boolean = False

            FromPg = 1

            ToPg = mTableFormat.NumPages + 1
            Dim PrintObj As PrintHandler = New PrintHandler(mTableFormat, mFormSel, FromPg, ToPg, Copies, Collate)
            PrintObj.PrintPreviewTable(PrintPreviewControl1)
        Catch ex As Exception
            Throw ex


        End Try
    End Sub

    Private Sub PrintGrid()
        Dim PrintSettings As New Printing.PrinterSettings
        Dim FromPg, ToPg As Int32
        Dim Copies As Int32 = 1
        Dim Collate As Boolean = False
        Dim Rows As Int32 = PrintPreviewControl1.Rows
        Dim Cols As Int32 = PrintPreviewControl1.Columns

        With PrintSettings
            .FromPage = 1
            .MinimumPage = 1
            .MaximumPage = mTableFormat.NumPages + 1
            .ToPage = mTableFormat.NumPages + 1
        End With
        With PrintDialog1
            .PrinterSettings = PrintSettings
            .AllowSelection = True
            .AllowSomePages = True
            If .ShowDialog(Me) = DialogResult.OK Then
                With PrintDialog1.PrinterSettings

                    Select Case .PrintRange
                        Case Drawing.Printing.PrintRange.AllPages
                            Debug.WriteLine("All")
                            FromPg = 1
                            ToPg = mTableFormat.NumPages + 1

                        Case Drawing.Printing.PrintRange.Selection
                            Debug.WriteLine("Selection")
                            FromPg = CInt(NumUpDownPages.Value)
                            ToPg = Math.Min(Rows * Cols + FromPg - 1, mTableFormat.NumPages + 1)

                        Case Drawing.Printing.PrintRange.SomePages
                            Debug.WriteLine("SomePages")
                            FromPg = .FromPage
                            ToPg = .ToPage

                    End Select
                    Collate = .Collate
                    Copies = .Copies
                End With
                Debug.WriteLine(String.Format("From {0}, To {1}, Copies {2}, Collate {3}", FromPg, ToPg, Copies, Collate))
                Dim PrintObj As New PrintHandler(mTableFormat, mFormSel, FromPg, ToPg, Copies, Collate)
                PrintObj.PrintTable()
            End If

            'Debug.WriteLine(String.Format("From page {0}, To page {1}", PrintSettings.FromPage, PrintSettings.ToPage))

        End With
    End Sub

    Private Sub PrintChart()
        Dim PrintSettings As New Printing.PrinterSettings
        Dim FromPg, ToPg As Int32
        Dim Copies As Int32 = 1
        Dim Collate As Boolean = False
        With PrintSettings

            .FromPage = 1
            .MinimumPage = 1

            .MaximumPage = mTableFormat.NumChartPages + 1
            .ToPage = mTableFormat.NumChartPages + 1
        End With

        With PrintDialog1
            .PrinterSettings = PrintSettings
            .AllowSelection = True
            .AllowSomePages = True



            If .ShowDialog(Me) = DialogResult.OK Then
                With .PrinterSettings

                    Select Case .PrintRange
                        Case Drawing.Printing.PrintRange.AllPages
                            Debug.WriteLine("All")
                            FromPg = 1
                            ToPg = mTableFormat.NumChartPages + 1

                        Case Drawing.Printing.PrintRange.Selection
                            Debug.WriteLine("Selection")
                            FromPg = mCurrChartPage + 1
                            ToPg = mCurrChartPage + 1

                        Case Drawing.Printing.PrintRange.SomePages
                            Debug.WriteLine("SomePages")
                            FromPg = .FromPage
                            ToPg = .ToPage

                    End Select
                    Collate = .Collate
                    Copies = .Copies
                End With
                Dim PrintObj As New PrintHandler(mTableFormat, mFormSel, FromPg, ToPg, Copies, Collate)
                PrintObj.PrintChart()

            End If

            Debug.WriteLine(String.Format("From page {0}, To page {1}", PrintSettings.FromPage, PrintSettings.ToPage))

        End With
    End Sub

    Private Sub cmdPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPrint.Click
        Try
            If mPrintChart Then
                PrintChart()
            Else
                PrintGrid()
            End If
        Catch ex As Exception
            MsgBox("An error occured: " & ex.Message)
            Debug.WriteLine(ex.ToString)

        End Try
    End Sub
End Class
0
 

Expert Comment

by:uinseann
ID: 10998289
Found this helpful.

You can use the PrintPreviewDialog, though.  Set your form's IsMDIContainer property to True.
Set the PreviewDialog's Parent to be your form.
Add a menu item to your form.
e.g. If you call the form frmPrintPreview, then you've got:

'(pdPreview is the name of my PreviewDialog)

Public Class frmPrintPreview
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "
#End Region
   
    Private WithEvents printDoc As New Printing.PrintDocument
    Private WithEvents fileMenuItem As New MenuItem("&File")
    Private WithEvents filePrintMenuItem As New MenuItem("&Print ...")

Private Sub filePrintMenuItem_Click(ByVal eventSender As System.Object, ByVal e As System.EventArgs) Handles filePrintMenuItem.Click

        Dim dlg As PrintDialog = New PrintDialog
        dlg.Document = printDoc
        dlg.AllowSomePages = True
       
        If dlg.ShowDialog() = DialogResult.OK Then
            printDoc.Print()
        End If

    End Sub


    Private Sub printDoc_PrintPage(ByVal eventSender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles printDoc.PrintPage

        Dim textToPrint As String = ".NET Printing example"
        Dim printFont As Font = New Font("Courier New", 16)

        Dim leftMargin As Integer = e.MarginBounds.Left
        Dim topMargin As Integer = e.MarginBounds.Top

        e.Graphics.DrawString(textToPrint, printFont, Brushes.Black, leftMargin, topMargin)

    End Sub

    Private Sub SetupPrintPreviewControl()

        fileMenuItem.MenuItems.Add(filePrintMenuItem)

        Me.Menu = New MainMenu
        Me.Menu.MenuItems.Add(fileMenuItem)

        Me.pdPreview.Document = Me.printDoc
        Me.WindowState = FormWindowState.Maximized
        With pdPreview
            .MdiParent = Me 'Main form must have IsMDIContainer set to True
            .WindowState = FormWindowState.Maximized  
            .Dock = DockStyle.Fill
            .MinimumSize = New System.Drawing.Size 'whatever u like here
            .ControlBox = False
            .Show()
        End With
         End Sub

'Initialise your control
Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
        SetupPrintPreviewControl()
    End Sub

'Close the main form, if you're closing the PreviewDialog
    Private Sub pdPreview_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles pdPreview.Leave
        Me.pdPreview.MdiParent.Close()
    End Sub

'Don't allow PreviewDialog resizing (you can use the main form instead)
    Private Sub pdPreview_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles pdPreview.Resize
        Me.pdPreview.WindowState = FormWindowState.Maximized
    End Sub

End Class
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now