Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi guys. PrintPreviewDialog print button printing directly. I want to open the PrintDialog screen and choose a printer. I found the code but I could not run it. The code I can run in C # does not work in VB.NET.


I need to run one of these two codes. Thanks in advance for your help.

What I have tried:

Code 1: This code is working in C#. VB.NET is not working.

Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click

        Dim b As New ToolStripButton
        b.Image = CType(PrintPreviewDialog1.Controls(1), ToolStrip).ImageList.Images(0)
        b.ToolTipText = "Print"
        b.DisplayStyle = ToolStripItemDisplayStyle.Image
        b.Click += printPreview_PrintClick() ' <--------  ERROR THIS LINE
        CType(PrintPreviewDialog1.Controls(1), ToolStrip).Items.RemoveAt(0)
        CType(PrintPreviewDialog1.Controls(1), ToolStrip).Items.Insert(0, b)

        PrintPreviewDialog1.StartPosition = FormStartPosition.CenterParent
        PrintPreviewDialog1.WindowState = FormWindowState.Maximized
        PrintPreviewDialog1.Icon = Icon
        PrintPreviewDialog1.ShowDialog()

    End Sub

Private Sub printPreview_PrintClick(sender As Object, e As EventArgs)
        Try
            PrintDialog1.Document = PrintDocument1
            If PrintDialog1.ShowDialog() = DialogResult.OK Then
                PrintDocument1.Print()
            End If
        Catch ex As Exception
        End Try
    End Sub


Code 2 :

 Private Sub PrintPreviewDialog1_Shown(sender As Object, e As EventArgs) Handles PrintPreviewDialog1.Shown

        Dim ts As ToolStrip = CType(Controls(1), ToolStrip) '<------- ERROR THIS LINE
        Dim printItem As ToolStripItem = ts.Items("printToolStripButton")
        With printItem
            Dim myPrintItem As ToolStripItem
            myPrintItem = ts.Items.Add(.Text, .Image, New EventHandler(AddressOf printPreview_PrintClick))
            myPrintItem.DisplayStyle = ToolStripItemDisplayStyle.Image
            ts.Items.Insert(0, myPrintItem)
        End With

        ts.Items.Remove(printItem)
    End Sub

Private Sub printPreview_PrintClick(sender As Object, e As EventArgs)
        Try
            PrintDialog1.Document = PrintDocument1
            If PrintDialog1.ShowDialog() = DialogResult.OK Then
                PrintDocument1.Print()
            End If
        Catch ex As Exception
        End Try
    End Sub
Posted
Updated 30-Aug-18 4:00am
Comments
Maciej Los 14-Sep-17 16:31pm    
There's a set of differences between vb.net and c#...

This line:
b.Click += printPreview_PrintClick()

is used to add handler to ToolStripButton.

I'd suggest to read this: AddHandler and RemoveHandler[^]
EssenceGold 14-Sep-17 17:29pm    
Thank You Maciej Los. thanks to your idea I've fixed the code. Code 1 now working fine.

AddHandler b.Click, AddressOf printPreview_PrintClick

Working Code:

Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click

        BufferImg()

        Dim b As New ToolStripButton
        b.Image = CType(PrintPreviewDialog1.Controls(1), ToolStrip).ImageList.Images(0)
        b.ToolTipText = "Print"
        b.DisplayStyle = ToolStripItemDisplayStyle.Image
        AddHandler b.Click, AddressOf printPreview_PrintClick ' <---RECPLACED CODE
        CType(PrintPreviewDialog1.Controls(1), ToolStrip).Items.RemoveAt(0)
        CType(PrintPreviewDialog1.Controls(1), ToolStrip).Items.Insert(0, b)

        PrintPreviewDialog1.StartPosition = FormStartPosition.CenterParent
        PrintPreviewDialog1.WindowState = FormWindowState.Maximized
        PrintPreviewDialog1.Icon = Icon
        PrintPreviewDialog1.ShowDialog()

    End Sub
 
Share this answer
 
v2
Well, EssenceGold's code does the print job well but it didn't preview any page/document.
So, I replaced some of your lines with mine.
VB
Private Sub Print_btn_Click(sender As Object, e As EventArgs) Handles Print_btn.Click
Dim b As New ToolStripButton
b.Image = CType(PrintPreviewDialog1.Controls(1), ToolStrip).ImageList.Images(0)
b.ToolTipText = "Print"
b.DisplayStyle = ToolStripItemDisplayStyle.Image
AddHandler b.Click, AddressOf PrintPreview_PrintClick
CType(PrintPreviewDialog1.Controls(1), ToolStrip).Items.RemoveAt(0)
CType(PrintPreviewDialog1.Controls(1), ToolStrip).Items.Insert(0, b)
'<--------- ADDED LINES------>
AddHandler PrintDocument1.PrintPage, AddressOf PrintDocument1_PrintPage
PrintPreviewDialog1.Document = PrintDocument1
PrintPreviewDialog1.ShowDialog()
End Sub
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900