SourceCode : Printing a Receipt in C# Forms

As many Regular Readers were demanding for this Basic Billing Solution. So, I have Added this script which i made yesterday night and pls feel free to ask me doubts 🙂

STEP 1 :

Create a new Project on VS

capture

STEP 2:

Add a listbox and 2 textbox and 2 buttons and thats it write code for buttons similar to this :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Printing;
using System.Data;

namespace erp
{
public partial class Form1 : Form
{
public Form1()
{

InitializeComponent();
}

private void btnprint_Click(object sender, EventArgs e)
{
PrintDialog printdialog = new PrintDialog();
PrintDocument printdocument = new PrintDocument();
printdialog.Document = printdocument;
printdocument.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(CreateRecipt);
DialogResult result = printdialog.ShowDialog();
if (result == DialogResult.OK)
{

printdocument.Print();
}
}
public void CreateRecipt(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
int total = 0;
float cash = float.Parse(txtCash.Text.Substring(1,3));

float change = 0.00f;
Graphics graphics = e.Graphics;
Font font = new Font("Courier New", 12);
float fontheight = font.GetHeight();
int startX = 10;
int startY = 10;
int offset = 40;
graphics.DrawString("Site4Help Inc.,", new Font("Courier New", 18), new SolidBrush(Color.Black), startX, startY);
string top = "Item Name".PadRight(15) + "Price";
graphics.DrawString(top,font,new SolidBrush(Color.Black),startX,startY + offset);
offset = offset + (int)fontheight;
graphics.DrawString("\n-----------------------------",font,new SolidBrush(Color.Black),startX,startY);
offset = offset + (int)fontheight + 5;
float totalprice = 0.00f;
foreach (string item in listBox1.Items)
{
string productDescription = item;
string productTotal = item.Substring(item.Length - 6, 6);
float productProce = float.Parse(item.Substring(item.Length - 5, 5));
totalprice += productProce;
string productLine = productDescription;
graphics.DrawString(productLine, new Font("Courier New", 12, FontStyle.Italic), new SolidBrush(Color.Black), startX, startY + offset);
offset = offset + (int)fontheight + 5;
}
change = (cash - totalprice);
offset = offset + 20;
graphics.DrawString("Total to Pay".PadRight(15) + string.Format("{0:c}", totalprice), new Font("Courier New", 12, FontStyle.Bold), new SolidBrush(Color.Black), startX, startY + offset);
}

private void button1_Click(object sender, EventArgs e)
{
listBox1.Items.Add(textBox1.Text.PadRight(15) + textBox2.Text);
textBox1.Text = "";
textBox2.Text = "";
}

private void Form1_Load(object sender, EventArgs e)
{

}
}
}

You can add as many elements you need without any Obligations and you can also improve this code as per your needs 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *