• Top Members
    Reps
    Posts
  • 834 Replies
    2585 Replies
  • 716 Replies
    2025 Replies
  • 331 Replies
    1928 Replies

Print Issue

Please LOCK your topics once you have found the solution to your question so we know you no longer require help with your query.
User avatar
Richardjmanning
Just Registered
Posts: 3

Print Issue

Thu Sep 07, 2017 3:23 pm

Hello guys,
Was wondering if someone could help me. I am trying to use pagesetup and printdocument and printsetupdialog all at once.
What i want is to be able to do change settings using pagesetup and apply this to my printdocument and then print this using printsetup
HOWEVER any code i try i have this weird error where every time i click a dialog it just opens anouther instance of itsself.

Could someone give me example code of how to do this please.

User avatar
CodenStuff
Site Admin
Posts: 4367

Re: Print Issue

Thu Sep 07, 2017 5:41 pm

Hello,

Welcome to the frustrating world of printing from winforms lol

I'm not sure how to best help you on this but I can point you in the direction of some excellent resources that may help you:

https://docs.microsoft.com/en-us/dotnet ... nt-support

And a good example of using the pagesetupdialog:

https://msdn.microsoft.com/en-us/librar ... -snippet-2

If possible could you post your code/source-files so I can take a look and try and find where your problem is.

..and welcome to the site :)
Welcome to CodenStuff.com Learn Code, Love Code. Thank you for being a member of the community.

User avatar
Richardjmanning
Just Registered
Posts: 3

Re: Print Issue

Fri Sep 08, 2017 10:14 am

Code: Select all

    Private Sub QR_Client_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        PrintDocument1.PrinterSettings = PageSetupDialog1.PrinterSettings
        PageSetupDialog1.Document = PrintDocument1
    End Sub

    Private Sub XylosButton2_Click(sender As Object, e As EventArgs) Handles XylosButton2.Click
        Try
            PageSetupDialog1.PageSettings = PrintDocument1.DefaultPageSettings
            PageSetupDialog1.PrinterSettings = PrintDocument1.PrinterSettings

            If PageSetupDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
                PrintDocument1.DefaultPageSettings = PageSetupDialog1.PageSettings
                PrintDocument1.PrinterSettings = PageSetupDialog1.PrinterSettings
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub XylosButton1_Click(sender As Object, e As EventArgs) Handles XylosButton1.Click
        Try
            PrintDocument1.PrinterSettings = PrintDocument1.PrinterSettings
            PrintDocument1.PrinterSettings = PrintDialog1.PrinterSettings()
            PrintDocument1.Print()
        Catch ex As Exception
            MsgBox("Printing Problem" & Chr(13) & ex.Message, MsgBoxStyle.Exclamation)
        End Try
    End Sub

    Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
        Using bmp As Bitmap = New Bitmap(QRCode.Image.Width, QRCode.Image.Height)

            Dim rect As New Rectangle(0, 0, QRCode.Image.Width, QRCode.Image.Height)
            Me.DrawToBitmap(bmp, rect)

            Dim l, t, w, h As Integer
            Dim ratio As Single = CSng(bmp.Width / bmp.Height)

            If ratio > e.MarginBounds.Width / e.MarginBounds.Height Then
                w = e.MarginBounds.Width
                h = CInt(w / ratio)
                t = CInt(e.MarginBounds.Top + (e.MarginBounds.Height / 2) - (h / 2))
                l = e.MarginBounds.Left
            Else
                h = e.MarginBounds.Height
                w = CInt(h * ratio)
                l = CInt(e.MarginBounds.Left + (e.MarginBounds.Width / 2) - (w / 2))
                t = e.MarginBounds.Top
            End If

            e.Graphics.DrawImage(bmp, l, t, w, h)
        End Using
    End Sub


User avatar
Richardjmanning
Just Registered
Posts: 3

Re: Print Issue

Fri Sep 08, 2017 10:44 am

I Dont really know how to explain the issue. Baysically. The print dialog shows up fine. But anytime i click in it at all even blank areas it just opens anouther print dialog

User avatar
CodenStuff
Site Admin
Posts: 4367

Re: Print Issue

Fri Sep 08, 2017 5:44 pm

Not sure what's causing the problem, the following code works fine for me:

Code: Select all

Private Sub XylosButton2_Click(sender As Object, e As EventArgs) Handles XylosButton2.Click
        Try
            PageSetupDialog1.PageSettings = PrintDocument1.DefaultPageSettings
            PageSetupDialog1.PrinterSettings = PrintDocument1.PrinterSettings

            If PageSetupDialog1.ShowDialog = DialogResult.OK Then
                PrintDocument1.DefaultPageSettings = PageSetupDialog1.PageSettings
                PrintDocument1.PrinterSettings = PageSetupDialog1.PrinterSettings
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
Welcome to CodenStuff.com Learn Code, Love Code. Thank you for being a member of the community.

Post Reply

Return to “Coding Help & Support”