Unable to execute the VB.Net code even can't view in Print Preview after clicked. Anyone there to help me. I'm new to VB.Net.

Imports System.Drawing.Imaging
'Imports IDAutomation.Windows.Forms.LinearBarCode
Imports System.Drawing.Printing
Imports System.Configuration
'Imports GenCode128
Imports System.Data.SqlClient


Public Class Form1

    Dim table2 As New DataTable
    Dim dte = Date.Now()

    Dim PrintDoc As New Printing.PrintDocument()
    Dim pd_PrintDialog As New PrintDialog
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

    Private Sub txtbarcode_TextChanged(sender As Object, e As EventArgs) Handles txtbarcode.TextChanged
        Dim Generator As New MessagingToolkit.Barcode.BarcodeEncoder

        Generator.BackColor = Color.Transparent
        Generator.LabelFont = New Font("Arial", 5, FontStyle.Regular)
        Generator.IncludeLabel = True
        Generator.CustomLabel = txtbarcode.Text
        Generator.Height = 100

        Try
            PbBarcode.Image = New Bitmap(Generator.Encode(MessagingToolkit.Barcode.BarcodeFormat.Code93, txtbarcode.Text))
        Catch
        End Try


    End Sub



    Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtbarcode.TextChanged
        PbBarcode.Image = Code128(txtbarcode.Text, "A")
    End Sub

    Private Sub btnprintbarcode_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnprintbarcode.Click
        'Barcode printing
        If txtbarcode.Text = "" Then 'Barcode is empty
            MsgBox("Please make sure you type the text to be converted to a barcode")
            Exit Sub
        Else

            pd_PrintDialog.UseEXDialog = True
            pd_PrintDialog.AllowPrintToFile = False




            If pd_PrintDialog.ShowDialog = Windows.Forms.DialogResult.OK Then

                PrintDoc.PrinterSettings = pd_PrintDialog.PrinterSettings



                AddHandler PrintDoc.PrintPage, AddressOf PrintDocHandler
                'Print
                PrintDoc.Print()

            ElseIf pd_PrintDialog.ShowDialog = Windows.Forms.DialogResult.Cancel Then
                Exit Sub

            End If
        End If
    End Sub
    'This is the event handler for printing bar codes
    Private Sub PrintDocHandler(ByVal sender As Object, ByVal ev As System.Drawing.Printing.PrintPageEventArgs)


        Dim rect As New Rectangle(50, 75, 750, 1000)
        Dim rect1 As New Rectangle(50, 75, 750, 1000)
        ev.Graphics.DrawString(txtpartnumber.Text, New Font("Arial", 5, FontStyle.Regular), Brushes.Black, rect)
        ev.Graphics.DrawString(txtqty.Text, New Font("Arial", 5, FontStyle.Regular), Brushes.Black, rect)
        ev.Graphics.DrawImage(PbBarcode.Image, rect1)


    End Sub

    Private Sub butPageSetup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


        PageSetupDialog1.Document = PrintDoc
        PageSetupDialog1.ShowDialog()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub btnpreview_Click(sender As Object, e As EventArgs) Handles btnpreview.Click
        AddHandler PrintDoc.PrintPage, AddressOf PrintDocHandler

        PrintDocument1.Document = PrintDoc
        PrintDocument1.ShowDialog()
    End Sub

End Class

1.PNG

Dani AI

Generated

Quick diagnostic and practical fixes for the missing part number / quantity and barcode in the preview (for ; echoing 's request to isolate the failing line).

Common causes seen in the posted code: the PrintPage handler is being attached repeatedly (handlers stack up), the same drawing rectangle is reused so text and image overwrite each other or are clipped, the PrintDialog.ShowDialog call is used twice, and the preview dialog may be pointed at a different PrintDocument instance than the one being printed. Verify the barcode PictureBox actually contains an Image before drawing.

Suggested workflow and minimal fixes:

  1. Attach the PrintPage handler once (for example in Form_Load) instead of adding it on every button click.
  2. Call the PrintDialog once and, if OK, copy its PrinterSettings to the PrintDocument and call Print.
  3. Point the preview dialog at the same PrintDocument instance that the PrintPage handler uses.
  4. Lay out text and image with separate coordinates so they don't overlap; set e.HasMorePages = False.

Minimal PrintPage example (VB.NET):

Private Sub PrintDoc_PrintPage(sender As Object, e As Printing.PrintPageEventArgs)
    Dim fTitle As New Font("Arial", 10, FontStyle.Bold)
    Dim fNormal As New Font("Arial", 10)
    e.Graphics.DrawString(txtPartNumber.Text, fTitle, Brushes.Black, New PointF(50.0F, 50.0F))
    e.Graphics.DrawString("Qty: " & txtQty.Text, fNormal, Brushes.Black, New PointF(50.0F, 80.0F))
    If PbBarcode.Image IsNot Nothing Then
        e.Graphics.DrawImage(PbBarcode.Image, New Rectangle(50, 110, 300, 80))
    End If
    e.HasMorePages = False
End Sub

If the goal is to print the whole GroupBox as it appears on-screen, render it to a bitmap and draw that bitmap:

Dim bmp As New Bitmap(GroupBox1.Width, GroupBox1.Height)
GroupBox1.DrawToBitmap(bmp, New Rectangle(0, 0, bmp.Width, bmp.Height))
e.Graphics.DrawImage(bmp, New PointF(50.0F, 50.0F))

Final tips: test with a trivial PrintPage that just draws a single string to confirm the preview works, remove duplicate AddHandler calls, and wrap PrintPage code in Try/Catch while debugging so any exceptions are visible instead of silently failing.

Recommended Answers

All 3 Replies

Sorry but what line fails with what error? Also, why tag this with VB6?

Unable to display the part number and quantity . I mean the group box in print preview or can't print too.

Private Sub PrintDocHandler(ByVal sender As Object, ByVal ev As System.Drawing.Printing.PrintPageEventArgs)


        Dim rect As New Rectangle(50, 75, 750, 1000)
        Dim rect1 As New Rectangle(50, 75, 750, 1000)
        ev.Graphics.DrawString(txtpartnumber.Text, New Font("Arial", 5, FontStyle.Regular), Brushes.Black, rect)
        ev.Graphics.DrawString(txtqty.Text, New Font("Arial", 5, FontStyle.Regular), Brushes.Black, rect)
        ev.Graphics.DrawImage(PbBarcode.Image, rect1)


    End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.