Solved

VB.Net - Print components in a Form

Posted on 2013-06-24
12
295 Views
Last Modified: 2014-01-24
Hi,
I have to print blank pages with some compay info.
The form information prints ok but it is also printing the form background (greyed).
Can someone help to maky this form only prints the desired information without the Form 'greyed' background?

This is the code I am using:

Private Sub btnPrintDatos_Click(sender As Object, e As EventArgs) Handles btnPrintDatos.Click

        If btnImpresora.Text = "Favor seleccione Impresora..." Then
            MsgBox("Favor seleccione una impresora")
        Else

            Dim folio_inicio, folio_final As Integer
            Dim impresora As String
            impresora = btnImpresora.Text
            folio_inicio = txtFolioInicio.Text
            folio_final = txtFolioFinal.Text

            For x = folio_inicio To folio_final

                txtFolioActual.Text = x
                'hacemos invisibles los controles que no deseamos que aparezcan
                btnPrintDatos.Visible = False
                lblFolioInicio.Visible = False
                lblFolioFinal.Visible = False
                txtFolioInicio.Visible = False
                txtFolioFinal.Visible = False
                lblImpresora.Visible = False
                btnImpresora.Visible = False

'I tried using the follow piece of code to hide the form background but it failed :(
                Me.TransparencyKey = Me.BackColor
                Me.AllowTransparency = True
                Me.BackColor = Me.TransparencyKey

                lblFolioActual.Focus()

                'actualizamos el formulario, para actualizar el folio actual
                PrintForm1.Form.Update()

                'configuramos la impresora
                Me.PrintForm1.PrinterSettings.PrinterName = impresora

                'define Margins
                PrintForm1.PrinterSettings.DefaultPageSettings.Margins = New System.Drawing.Printing.Margins(0, 0, 0, 0)

'we now print!
PrintForm1.Print(Me, PowerPacks.Printing.PrintForm.PrintOption.CompatibleModeClientAreaOnly)


                'hacemos visible los controles nuevamente
                Me.Visible = True
                btnPrintDatos.Visible = True
                lblFolioInicio.Visible = True
                lblFolioFinal.Visible = True
                txtFolioInicio.Visible = True
                txtFolioFinal.Visible = True
                lblImpresora.Visible = True
                btnImpresora.Visible = True

            Next x
            MsgBox("Impresion terminada hermano, que tengas un gran día :)")
        End If

    End Sub

    Private Sub btnImpresora_Click(sender As Object, e As EventArgs) Handles btnImpresora.Click
        Dim impresora As String = PrintDialog1.ShowDialog()
        Dim impresora_nombre As String = PrintDialog1.PrinterSettings.PrinterName
        btnImpresora.Text = impresora_nombre
    End Sub

Open in new window

0
Comment
Question by:José Perez
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
12 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 39274029
change the background of the form to white just before the print, and restore its background afterwards.
0
 
LVL 2

Author Comment

by:José Perez
ID: 39311844
It didn't work. The background have not changed anything. I did it but it is still printing in gray :(
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39313222
let me try i'll get back to you
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 42

Expert Comment

by:sedgwick
ID: 39313424
use this code:
Private Sub btnPrintDatos_Click(sender As Object, e As EventArgs) Handles btnPrintDatos.Click

        If btnImpresora.Text = "Favor seleccione Impresora..." Then
            MsgBox("Favor seleccione una impresora")
        Else

            Dim folio_inicio, folio_final As Integer
            Dim impresora As String
            impresora = btnImpresora.Text
            folio_inicio = txtFolioInicio.Text
            folio_final = txtFolioFinal.Text

            For x = folio_inicio To folio_final

                txtFolioActual.Text = x
                'hacemos invisibles los controles que no deseamos que aparezcan
                btnPrintDatos.Visible = False
                lblFolioInicio.Visible = False
                lblFolioFinal.Visible = False
                txtFolioInicio.Visible = False
                txtFolioFinal.Visible = False
                lblImpresora.Visible = False
                btnImpresora.Visible = False

'I tried using the follow piece of code to hide the form background but it failed :(
                Me.TransparencyKey = Me.BackColor
                Me.AllowTransparency = True
                Me.BackColor = Me.TransparencyKey

                lblFolioActual.Focus()

                'actualizamos el formulario, para actualizar el folio actual
                PrintForm1.Form.Update()

                'configuramos la impresora
                Me.PrintForm1.PrinterSettings.PrinterName = impresora

                'define Margins
                PrintForm1.PrinterSettings.DefaultPageSettings.Margins = New System.Drawing.Printing.Margins(0, 0, 0, 0)

dim defaultColor As Color = Me.Background
Me.BackColor = Color.White
Me.Update()
'we now print!
PrintForm1.Print(Me, PowerPacks.Printing.PrintForm.PrintOption.ClientAreaOnly)
Me.BackColor = defaultColor 
Me.Update()

                'hacemos visible los controles nuevamente
                Me.Visible = True
                btnPrintDatos.Visible = True
                lblFolioInicio.Visible = True
                lblFolioFinal.Visible = True
                txtFolioInicio.Visible = True
                txtFolioFinal.Visible = True
                lblImpresora.Visible = True
                btnImpresora.Visible = True

            Next x
            MsgBox("Impresion terminada hermano, que tengas un gran día :)")
        End If

    End Sub

    Private Sub btnImpresora_Click(sender As Object, e As EventArgs) Handles btnImpresora.Click
        Dim impresora As String = PrintDialog1.ShowDialog()
        Dim impresora_nombre As String = PrintDialog1.PrinterSettings.PrinterName
        btnImpresora.Text = impresora_nombre
    End Sub

Open in new window

0
 
LVL 2

Author Comment

by:José Perez
ID: 39314185
It displays an error on "Me.Background". It says "Not a memeber of Form1"

'define Margins
                PrintForm1.PrinterSettings.DefaultPageSettings.Margins = New System.Drawing.Printing.Margins(0, 0, 0, 0)

                Dim defaultColor As Color = Me.Background 'here is the error displayed

Open in new window

0
 
LVL 42

Accepted Solution

by:
sedgwick earned 250 total points
ID: 39314247
change Background to Backcolor
0
 
LVL 2

Author Comment

by:José Perez
ID: 39314537
mmm it fixed the background issue but the text is been printed 'blurry'
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39314611
i actually try this code with our office printer and it looks fine.
do the font looks different when u print in grey rather than in white background?
0
 
LVL 2

Author Comment

by:José Perez
ID: 39314683
no, it sees the same but when the form was printing in gray I didn't notice.

If I attached a zip file for you to try in your printer would please check?
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39316736
sure
0
 
LVL 2

Author Comment

by:José Perez
ID: 39317254
It did not allow me to upload the file due to site restrictions so I uplodad a pdf rendition of the final printing. There you can also see that it is 'blurry'.
1.pdf
0
 
LVL 2

Author Closing Comment

by:José Perez
ID: 39806512
Finally the text was blurry but it works.
Thanks a lot!
0

Featured Post

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

733 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