-
Nov 14th, 2010, 12:47 PM
#1
Thread Starter
Frenzied Member
Printing example, short, simple & easy.
Here is some code I converted from VB.NET to C# from my original VB.NET code bank submission which can be found here.
Commented code:
Code:
//NOTE * // = opening comments and \\ = closing comments...
public class Form1
{
//Declares public variables...
public string printer;
public int copies;
private void cmdPrint_Click(System.Object sender, System.EventArgs e)
{
myPrinter Print = new myPrinter();
//Declares Print as a new myPrinter class.
PrintDialog myprintdialog = new PrintDialog();
//Creates Print Dialog.
PrintDialog _with1 = myprintdialog;
if (_with1.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
////------
printer = _with1.PrinterSettings.PrinterName;
//Sets variable printer to selected
//printers name.
//\\------
////------
copies = _with1.PrinterSettings.Copies;
//Sets ammount of copies to number specified
// in dialog.
//\\------
////------
Print.prt(rtbText.Text.Trim);
//calls the prt sub in
//the myPrinter class with text (a string) set to the text in the rtb.
//Basicly starts the printing process...
//\\------
}
}
}
//Print Class
public class myPrinter
{
//Declares TextToBePrinted as a string.
private string TextToBePrinted;
//Below is the void that prints the text to the printer.
public void prt(string text)
{
TextToBePrinted = text;
Printing.PrintDocument prn = new Printing.PrintDocument();
using ((prn)) {
prn.PrinterSettings.PrinterName = Form1.printer;
prn.PrinterSettings.Copies = Form1.copies;
//// Adds a handler for PrintDocument.PrintPage
//(the sub PrintPageHandler)
prn.PrintPage += this.PrintPageHandler;
//\\
prn.Print();
//Prints.
//// Removes the handler for PrintDocument.PrintPage
//(the sub PrintPageHandler)
prn.PrintPage -= this.PrintPageHandler;
//\\
}
}
//Below is code that sets the fonts etc...
private void PrintPageHandler(object sender, Printing.PrintPageEventArgs args)
{
Font myFont = new Font("Microsoft San Serif", 10);
args.Graphics.DrawString(TextToBePrinted, new Font(myFont, FontStyle.Regular), Brushes.Black, 50, 50);
}
}
Uncommented Code:
Code:
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
public class Form1
{
public string printer;
public int copies;
private void cmdPrint_Click(System.Object sender, System.EventArgs e)
{
myPrinter Print = new myPrinter();
PrintDialog myprintdialog = new PrintDialog();
PrintDialog _with1 = myprintdialog;
if (_with1.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
printer = _with1.PrinterSettings.PrinterName;
copies = _with1.PrinterSettings.Copies;
Print.prt(rtbText.Text.Trim);
}
}
}
public class myPrinter
{
private string TextToBePrinted;
public void prt(string text)
{
TextToBePrinted = text;
Printing.PrintDocument prn = new Printing.PrintDocument();
using ((prn)) {
prn.PrinterSettings.PrinterName = Form1.printer;
prn.PrinterSettings.Copies = Form1.copies;
prn.PrintPage += this.PrintPageHandler;
prn.Print();
prn.PrintPage -= this.PrintPageHandler;
}
}
private void PrintPageHandler(object sender, Printing.PrintPageEventArgs args)
{
Font myFont = new Font("Microsoft San Serif", 10);
args.Graphics.DrawString(TextToBePrinted, new Font(myFont, FontStyle.Regular), Brushes.Black, 50, 50);
}
}
If you find any errors in this code, have any suggestions, questions or comments just feel free to post below - it is a forum after all.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|