How to use Printing Controls in VB.NET??

Printing Controls provide a great way to programmer to perform Print Related Tasks.
We need to draw following controls on our Windows Form

  1. PrintDialog
  2. PrintDocument
  3. PrintPreviewDialog
  4. Rich Text Box
A PrintDialog control is used to open the WINDOWS Print Dialog. The PrintDialog allows user to send and output to Pinter. With the help of Print Preview Dialog, you can preview what and how it is going to be printed

How to Print Document and Check Print Preview in VB.NET

Print Document
The PrintDocument objects holds all the information that is required to print a Pag. THey are to be associated with the control whose contents are to be printed. PrintDocument handles the concerned EVENTS and OPERATIONS

We need to handle the Print_Page event of the Print Document. Write following statements at Print_Page Event of Print Document1

  1.      e.Graphics.DrawString(RichTextBox1.Text, RichTextBox1.Font, Brushes.Black, 100,100)
  2.      e. Graphics.PageUnit=GraphicsUnit.Inch

Explanation of Above two statements
Statement (1) is used to draw specified string with specified brush in an area of Specified Position Coordinates. The General format for this Method is:
                                e.Graphics.DrawString(s,font,brush,single, single)
Here, s is the String to be drawn. We may have any Textbox, RichTextBox, Label Data as String
          font, specifies the Text format of the String. We can set it to the concerned Control's Font 
          Brush determines the color and other format for the String to be drawn
          Single, creates a X point at upper left corner to draw the string
          single , creates a Y point at Upper left corner to draw the string

Statement (2) is used to get or set the unit of measurement for Page Coordinates in the current Graphics. We have used here the 'Inch' unit.

Now, we move on to the PRINT  button and write following codes
          if  PrintDialog1.ShowDialog()= DialogRsult.Ok Then
              PrintDocument1.Print()
          endif

And Finally, the PrintPreview Button
             PrintPreviewDialog1.Document=PrintDocument1
              PrintPreviewDialog1.showDialog()


I hope this would solve you initial Print Related Problems in VB.NET. Do write if you need anything else in this concern

Bye, Have a Nice Day....



Comments

Popular posts from this blog

Understanding Working of Text & Binary File in C

List of C Programs that makes u understand concepts in an easy fashion(Part1)

Starting C : A series of Chapters to learn C