Printing

can some body tell me the easy way to print in Vb.net , i don't want to use the cristal reports

LVL 1
cano63Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MacNuttinCommented:
Imports System.Drawing.Printing

'In here goes your code

Private WithEvents mDoc As New PrintDocument()
  Private mFont As New Font("Courier New", 12)

  Private Sub btnSimple_Click(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles btnSimple.Click

    Dim dlg As New PrintPreviewDialog()
    dlg.Document = mDoc
    dlg.ShowDialog()

    'mDoc.Print()

  End Sub

  Private Sub mDoc_PrintPage(ByVal sender As Object, _
      ByVal e As System.Drawing.Printing.PrintPageEventArgs) _
      Handles mDoc.PrintPage

    Dim xPos As Single = e.MarginBounds.Left
    Dim yPos As Single = e.MarginBounds.Top
    Dim lineHeight As Single = mFont.GetHeight(e.Graphics)

    With e.Graphics
      .DrawString("This is a test", _
        mFont, Brushes.Black, xPos, yPos)
      yPos += lineHeight
      .DrawString("This is another test", _
        mFont, Brushes.Black, xPos, yPos)
      xPos += .MeasureString("This is another test", mFont).Width
      .DrawString("Here's some more text", _
        mFont, Brushes.Black, xPos, yPos)
      xPos = e.MarginBounds.Left
    End With

  End Sub

  Private Sub mDoc_BeginPrint(ByVal sender As Object, _
      ByVal e As System.Drawing.Printing.PrintEventArgs) _
      Handles mDoc.BeginPrint

  End Sub

  Private Sub mDoc_EndPrint(ByVal sender As Object, _
      ByVal e As System.Drawing.Printing.PrintEventArgs) _
      Handles mDoc.EndPrint

  End Sub

  Private Sub btnPrintDialog_Click(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles btnPrintDialog.Click

    Dim dlg As New PrintDialog()
    dlg.Document = mDoc
    'dlg.AllowPrintToFile = True
    'dlg.AllowSelection = True
    'dlg.AllowSomePages = True
    If dlg.ShowDialog = DialogResult.OK Then
      mDoc.Print()
    End If
  End Sub

  Private Sub btnPageDialog_Click(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles btnPageDialog.Click

    Dim dlg As New PageSetupDialog()
    dlg.Document = mDoc
    'dlg.AllowMargins = False
    'dlg.AllowOrientation = False
    'dlg.AllowPaper = False
    'dlg.AllowPrinter = False
    dlg.ShowDialog()

  End Sub

  Private Sub btnPreview_Click(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles btnPreview.Click

    Dim dlg As New PrintPreviewDialog()
    dlg.Document = mDoc
    dlg.WindowState = FormWindowState.Maximized
    dlg.ShowDialog()

  End Sub

  Private Sub btnPreviewControl_Click(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles btnPreviewControl.Click

    'With mDoc.PrinterSettings
    '  .PrintRange = PrintRange.SomePages
    '  .FromPage = 1
    '  .ToPage = 1
    'End With

    prevDisplay.Document = mDoc

  End Sub

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cano63Author Commented:
I would like to know why microsoft change printer.print process?
PeterdLoCommented:
Hi cano63,

According to Microsoft, the reason of removing printer.print process from VB6 is to simplify printing programming and provide a unified approach for generating all types of printouts (in .NET). The detailed explanation can be found in the MSDN library under Visual Basic & related with the subject of Printing changes in Visual Basic .NET. Mainly, it added PageSetupDialog and PrintPreviewDialog controls. With all claimed benefits in .NET, I still think the VB6 printing is more developer friendly and easily. Do you agree? Have a good day!
cano63Author Commented:
Yes i agree
cano63Author Commented:
what about the data reports why they remove it they works fine for me
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.