Visual Basic Development Bookmark and Share   
 Home > Visual Basic General > Printing in vb express 2008
 

Printing in vb express 2008

I am working in vb express 2008. I got some sample code from the site www.vbhelper.com and tried to implement it as it is. Debugging of the code gave following errors:

Error1'Protected Overrides Sub Dispose(disposing As Boolean)' has multiple definitions with identical signatures.C:\Documents and Settings\DaLicaRestaurant\Local Settings\Application Data\Temporary Projects\Printing\Form1.Designer.vb729Printing
Error2'Private Sub InitializeComponent()' has multiple definitions with identical signatures.C:\Documents and Settings\DaLicaRestaurant\Local Settings\Application Data\Temporary Projects\Printing\Form1.Designer.vb2417Printing
Error3'components' is already declared as 'Private components As System.ComponentModel.IContainer' in this class.C:\Documents and Settings\DaLicaRestaurant\Local Settings\Application Data\Temporary Projects\Printing\Form1.vb2913Printing

The sample Code i copied is as under:
Imports System.Drawing.Printing

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents btnPrintPreview As System.Windows.Forms.Button
    Friend WithEvents dlgPrint As System.Windows.Forms.PrintDialog
    Friend WithEvents dlgPrintPreview As System.Windows.Forms.PrintPreviewDialog
    Friend WithEvents btnPrintNow As System.Windows.Forms.Button
    Friend WithEvents btnPrintWithDialog As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
        Me.btnPrintPreview = New System.Windows.Forms.Button
        Me.btnPrintWithDialog = New System.Windows.Forms.Button
        Me.dlgPrint = New System.Windows.Forms.PrintDialog
        Me.dlgPrintPreview = New System.Windows.Forms.PrintPreviewDialog
        Me.btnPrintNow = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'btnPrintPreview
        '
        Me.btnPrintPreview.Location = New System.Drawing.Point(104, 80)
        Me.btnPrintPreview.Name = "btnPrintPreview"
        Me.btnPrintPreview.Size = New System.Drawing.Size(96, 23)
        Me.btnPrintPreview.TabIndex = 0
        Me.btnPrintPreview.Text = "Print Preview"
        '
        'btnPrintWithDialog
        '
        Me.btnPrintWithDialog.Location = New System.Drawing.Point(104, 120)
        Me.btnPrintWithDialog.Name = "btnPrintWithDialog"
        Me.btnPrintWithDialog.Size = New System.Drawing.Size(96, 23)
        Me.btnPrintWithDialog.TabIndex = 1
        Me.btnPrintWithDialog.Text = "Print w/Dialog"
        '
        'dlgPrintPreview
        '
        Me.dlgPrintPreview.AutoScrollMargin = New System.Drawing.Size(0, 0)
        Me.dlgPrintPreview.AutoScrollMinSize = New System.Drawing.Size(0, 0)
        Me.dlgPrintPreview.ClientSize = New System.Drawing.Size(400, 300)
        Me.dlgPrintPreview.Enabled = True
        Me.dlgPrintPreview.Icon = CType(resources.GetObject("dlgPrintPreview.Icon"), System.Drawing.Icon)
        Me.dlgPrintPreview.Location = New System.Drawing.Point(96, 16)
        Me.dlgPrintPreview.MinimumSize = New System.Drawing.Size(375, 250)
        Me.dlgPrintPreview.Name = "dlgPrintPreview"
        Me.dlgPrintPreview.TransparencyKey = System.Drawing.Color.Empty
        Me.dlgPrintPreview.Visible = False
        '
        'btnPrintNow
        '
        Me.btnPrintNow.Location = New System.Drawing.Point(104, 160)
        Me.btnPrintNow.Name = "btnPrintNow"
        Me.btnPrintNow.Size = New System.Drawing.Size(96, 23)
        Me.btnPrintNow.TabIndex = 2
        Me.btnPrintNow.Text = "Print Now"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 273)
        Me.Controls.Add(Me.btnPrintNow)
        Me.Controls.Add(Me.btnPrintWithDialog)
        Me.Controls.Add(Me.btnPrintPreview)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region

    ' Display a print preview.
    Private Sub btnPrintPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrintPreview.Click
        ' Make a PrintDocument and attach it to 
        ' the PrintPreview dialog.
        dlgPrintPreview.Document = PreparePrintDocument()

        ' Preview.
        dlgPrintPreview.WindowState = FormWindowState.Maximized
        dlgPrintPreview.ShowDialog()
    End Sub

    ' Print with the print dialog.
    Private Sub btnPrintWithDialog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrintWithDialog.Click
        ' Make a PrintDocument and attach it to 
        ' the Print dialog.
        dlgPrint.Document = PreparePrintDocument()

        ' Display the print dialog.
        dlgPrint.ShowDialog()
    End Sub

    ' Print immediately.
    Private Sub btnPrintNow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrintNow.Click
        ' Make a PrintDocument object.
        Dim print_document As PrintDocument = PreparePrintDocument()

        ' Print immediately.
        print_document.Print()
    End Sub

    ' Make and return a PrintDocument object that's ready
    ' to print the paragraphs.
    Private Function PreparePrintDocument() As PrintDocument
        ' Make the PrintDocument object.
        Dim print_document As New PrintDocument

        ' Install the PrintPage event handler.
        AddHandler print_document.PrintPage, AddressOf Print_PrintPage

        ' Return the object.
        Return print_document
    End Function

    ' Print the next page.
    Private Sub Print_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
        ' Draw a rectangle at the margins.
        e.Graphics.DrawRectangle(Pens.Black, e.MarginBounds)

        ' Draw a thick, dashed ellipse.
        Dim dotted_pen As New Pen(Color.Black, 5)
        dotted_pen.DashStyle = Drawing2D.DashStyle.Dash
        e.Graphics.DrawEllipse(dotted_pen, e.MarginBounds)
        dotted_pen.Dispose()

        ' Draw a thick diamond.
        Dim x0 As Integer = e.MarginBounds.X
        Dim y0 As Integer = e.MarginBounds.Y
        Dim wid As Integer = e.MarginBounds.Width
        Dim hgt As Integer = e.MarginBounds.Height
        Dim pts() As Point = { _
            New Point(x0, y0 + hgt \ 2), _
            New Point(x0 + wid \ 2, y0), _
            New Point(x0 + wid, y0 + hgt \ 2), _
            New Point(x0 + wid \ 2, y0 + hgt) _
        }
        e.Graphics.DrawPolygon(New Pen(Color.Black, 5), pts)

        ' There are no more pages.
        e.HasMorePages = False
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
End Class


Please advise what i am doing so wrong with this code. Thanks
sazd1  19 hours 54 minutes ago
That could be because "Windows Form Designer generated code" would already be there in Form1.Designer.vb.

Try following code


Imports System.Drawing.Printing

Public Class Form1
    Inherits System.Windows.Forms.Form


    ' Display a print preview.
    Private Sub btnPrintPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrintPreview.Click
        ' Make a PrintDocument and attach it to 
        ' the PrintPreview dialog.
        dlgPrintPreview.Document = PreparePrintDocument()

        ' Preview.
        dlgPrintPreview.WindowState = FormWindowState.Maximized
        dlgPrintPreview.ShowDialog()
    End Sub

    ' Print with the print dialog.
    Private Sub btnPrintWithDialog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrintWithDialog.Click
        ' Make a PrintDocument and attach it to 
        ' the Print dialog.
        dlgPrint.Document = PreparePrintDocument()

        ' Display the print dialog.
        dlgPrint.ShowDialog()
    End Sub

    ' Print immediately.
    Private Sub btnPrintNow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrintNow.Click
        ' Make a PrintDocument object.
        Dim print_document As PrintDocument = PreparePrintDocument()

        ' Print immediately.
        print_document.Print()
    End Sub

    ' Make and return a PrintDocument object that's ready
    ' to print the paragraphs.
    Private Function PreparePrintDocument() As PrintDocument
        ' Make the PrintDocument object.
        Dim print_document As New PrintDocument

        ' Install the PrintPage event handler.
        AddHandler print_document.PrintPage, AddressOf Print_PrintPage

        ' Return the object.
        Return print_document
    End Function

    ' Print the next page.
    Private Sub Print_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
        ' Draw a rectangle at the margins.
        e.Graphics.DrawRectangle(Pens.Black, e.MarginBounds)

        ' Draw a thick, dashed ellipse.
        Dim dotted_pen As New Pen(Color.Black, 5)
        dotted_pen.DashStyle = Drawing2D.DashStyle.Dash
        e.Graphics.DrawEllipse(dotted_pen, e.MarginBounds)
        dotted_pen.Dispose()

        ' Draw a thick diamond.
        Dim x0 As Integer = e.MarginBounds.X
        Dim y0 As Integer = e.MarginBounds.Y
        Dim wid As Integer = e.MarginBounds.Width
        Dim hgt As Integer = e.MarginBounds.Height
        Dim pts() As Point = { _
            New Point(x0, y0 + hgt \ 2), _
            New Point(x0 + wid \ 2, y0), _
            New Point(x0 + wid, y0 + hgt \ 2), _
            New Point(x0 + wid \ 2, y0 + hgt) _
        }
        e.Graphics.DrawPolygon(New Pen(Color.Black, 5), pts)

        ' There are no more pages.
        e.HasMorePages = False
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
End Class


Gaurav Khanna
Khanna Gaurav  18 hours 32 minutes ago
It sounds like you downloaded the project and then copied and pasted the code into a new project. THat's a good approach but as Khanna Gaurav says, in VB 2008 the windows forms designer generated code is in the separate file Form1.Designer.vb. His code shown here only includes the non-automatically generated code.

Another approach would be to simply open the example project. VB 2008 should upgrade it to make the changes you need.

Then if you want to start a new project, you can copy and paste the controls from the form designer and the non-UI building code from the code editor.

Rod
www.vb-helper.com
Rod Stephens  17 hours 40 minutes ago

You can use google to search for other answers

Custom Search

More Threads

• When I scroll through multiple records using the Binding Navigator, how do I get my information to stay in one record?
• Multiple ContextMenuStrips for DataGridView?
• SQL Compact Server Question
• How to halt a program???
• Name of Editor ??
• application switch event
• Problem With DataRow
• Hidden list controls don't set SelectedValue
• Compilation problem when add reference of another dll
• help---> oleDbDataAdapter