?
Solved

VB.Net - Print components in a Form

Posted on 2013-06-24
12
Medium Priority
?
297 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
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 500 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

752 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