wangoland

CODE : Receipt Printing Demo - C# With Windows Forms

Jul 8th, 2014
29,854
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.46 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Data;
  11. using System.Drawing.Printing;
  12. using Microsoft.VisualBasic;
  13.  
  14. namespace Reciept_Print_Test
  15. {
  16.     public partial class Form1 : Form
  17.     {
  18.         public Form1()
  19.         {
  20.             InitializeComponent();
  21.         }
  22.  
  23.         private void btnAddItem_Click(object sender, EventArgs e)
  24.         {
  25.             listBox1.Items.Add(txtName.Text.PadRight(30) + txtPrice.Text);
  26.             txtName.Text = "";
  27.             txtPrice.Text = "";
  28.         }
  29.  
  30.         private void btnRemoveItem_Click(object sender, EventArgs e)
  31.         {
  32.             try
  33.             {
  34.                 listBox1.Items.RemoveAt(listBox1.SelectedIndex);
  35.             }
  36.             catch (Exception)
  37.             {
  38.  
  39.                 return;
  40.             }
  41.         }
  42.  
  43.         private void btnPrintReciept_Click(object sender, EventArgs e)
  44.         {
  45.             PrintDialog printDialog = new PrintDialog();
  46.  
  47.             PrintDocument printDocument = new PrintDocument();
  48.  
  49.             printDialog.Document = printDocument; //add the document to the dialog box...        
  50.  
  51.             printDocument.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(CreateReceipt); //add an event handler that will do the printing
  52.  
  53.             //on a till you will not want to ask the user where to print but this is fine for the test envoironment.
  54.  
  55.             DialogResult result = printDialog.ShowDialog();
  56.  
  57.             if (result == DialogResult.OK)
  58.             {
  59.             printDocument.Print();
  60.  
  61.             }
  62.         }
  63.  
  64.         public void CreateReceipt(object sender, System.Drawing.Printing.PrintPageEventArgs e)
  65.         {
  66.            
  67.             int total = 0;
  68.             float cash = float.Parse(txtCash.Text.Substring(1,5));
  69.             float change = 0.00f;
  70.            
  71.             //this prints the reciept
  72.  
  73.             Graphics graphic = e.Graphics;
  74.  
  75.             Font font = new Font("Courier New", 12); //must use a mono spaced font as the spaces need to line up
  76.  
  77.             float fontHeight = font.GetHeight();
  78.  
  79.             int startX = 10;
  80.             int startY = 10;
  81.             int offset = 40;
  82.  
  83.             graphic.DrawString(" Wangoland Coffee Shop", new Font("Courier New", 18), new SolidBrush(Color.Black), startX, startY);
  84.             string top = "Item Name".PadRight(30) + "Price";
  85.             graphic.DrawString(top, font, new SolidBrush(Color.Black), startX, startY + offset);
  86.             offset = offset + (int)fontHeight; //make the spacing consistent
  87.             graphic.DrawString("----------------------------------", font, new SolidBrush(Color.Black), startX, startY + offset);
  88.             offset = offset + (int)fontHeight + 5; //make the spacing consistent
  89.  
  90.             float totalprice = 0.00f;
  91.  
  92.             foreach (string item in listBox1.Items)
  93.             {
  94.                 //create the string to print on the reciept
  95.                 string productDescription = item;
  96.                 string productTotal = item.Substring(item.Length - 6, 6);
  97.                 float productPrice = float.Parse(item.Substring(item.Length - 5, 5));
  98.  
  99.                 //MessageBox.Show(item.Substring(item.Length - 5, 5) + "PROD TOTAL: " + productTotal);
  100.                
  101.  
  102.                 totalprice += productPrice;
  103.  
  104.                 if (productDescription.Contains("  -"))
  105.                 {
  106.                     string productLine = productDescription.Substring(0,24);
  107.  
  108.                     graphic.DrawString(productLine, new Font("Courier New", 12, FontStyle.Italic), new SolidBrush(Color.Red), startX, startY + offset);
  109.  
  110.                     offset = offset + (int)fontHeight + 5; //make the spacing consistent
  111.                 }
  112.                 else
  113.                 {
  114.                     string productLine = productDescription;
  115.  
  116.                     graphic.DrawString(productLine, font, new SolidBrush(Color.Black), startX, startY + offset);
  117.  
  118.                     offset = offset + (int)fontHeight + 5; //make the spacing consistent
  119.                 }
  120.  
  121.             }
  122.  
  123.             change = (cash - totalprice);
  124.  
  125.             //when we have drawn all of the items add the total
  126.  
  127.             offset = offset + 20; //make some room so that the total stands out.
  128.  
  129.             graphic.DrawString("Total to pay ".PadRight(30) + String.Format("{0:c}", totalprice), new Font("Courier New", 12, FontStyle.Bold), new SolidBrush(Color.Black), startX, startY + offset);
  130.  
  131.             offset = offset + 30; //make some room so that the total stands out.
  132.             graphic.DrawString("CASH ".PadRight(30) + String.Format("{0:c}", cash), font, new SolidBrush(Color.Black), startX, startY + offset);
  133.             offset = offset + 15;
  134.             graphic.DrawString("CHANGE ".PadRight(30) + String.Format("{0:c}", change), font, new SolidBrush(Color.Black), startX, startY + offset);
  135.             offset = offset + 30; //make some room so that the total stands out.
  136.             graphic.DrawString("     Thank-you for your custom,", font, new SolidBrush(Color.Black), startX, startY + offset);
  137.             offset = offset + 15;
  138.             graphic.DrawString("       please come back soon!", font, new SolidBrush(Color.Black), startX, startY + offset);
  139.  
  140.         }
  141.  
  142.         private void label2_Click(object sender, EventArgs e)
  143.         {
  144.  
  145.         }
  146.     }
  147. }
Add Comment
Please, Sign In to add comment