I need help with printing support. i followed a tutorial on dreamincode.net for printing and now i need help. When i print, it prints out two pages the first one has 0 in it and the second one has 00 on it. I am trying to get it to print out like this:
[Text Centered] Formula Name
[Text Centered] Formula Number
----------------------------------------------------------------------------------------
Items from lstResult [Tab] [Tab] Values from lstResult2
Total:
This is my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Printing;
namespace Perfume_Formula_Typing_Software
{
public partial class Form1 : Form
{
List<double> values;
public Form1()
{
InitializeComponent();
}
public class PCPrint :
System.Drawing.Printing.PrintDocument
{
private Font _font;
private string _text;
public string TextToPrint
{
get { return _text; }
set { _text = value; }
}
public Font PrinterFont
{
get { return _font; }
set { _font = value; }
}
static int curChar;
public PCPrint()
: base()
{
_text = string.Empty;
}
public PCPrint(string str)
: base()
{
_text = str;
}
protected override void OnBeginPrint(PrintEventArgs e)
{
base.OnBeginPrint(e);
if (_font == null)
{
_font = new Font("Times New Roman", 12);
}
}
protected override void OnPrintPage(System.Drawing.Printing.PrintPageEventArgs e)
{
base.OnPrintPage(e);
int printHeight;
int printWidth;
int leftMargin;
int rightMargin;
Int32 lines;
Int32 chars;
{
printHeight = base.DefaultPageSettings.PaperSize.Height
- base.DefaultPageSettings.Margins.Top -
base.DefaultPageSettings.Margins.Bottom;
printWidth = base.DefaultPageSettings.PaperSize.Width
- base.DefaultPageSettings.Margins.Left - base.DefaultPageSettings.Margins.Right;
leftMargin = base.DefaultPageSettings.Margins.Left;
rightMargin = base.DefaultPageSettings.Margins.Top;
}
if (base.DefaultPageSettings.Landscape)
{
int tmp;
tmp = printHeight;
printHeight = printWidth;
printWidth = tmp;
}
Int32 numLines = (int)printHeight / PrinterFont.Height;
RectangleF printArea = new
RectangleF(leftMargin, rightMargin, printWidth, printHeight);
StringFormat format = new StringFormat(StringFormatFlags.LineLimit);
e.Graphics.MeasureString
(_text.Substring(RemoveZeros(curChar)),
PrinterFont, new SizeF(printWidth,
printHeight), format, out chars, out
lines);
e.Graphics.DrawString(_text.Substring(RemoveZeros(curChar)), PrinterFont, Brushes.Black, printArea, format);
curChar += chars;
if (curChar < _text.Length)
{
e.HasMorePages = true;
}
else
{
e.HasMorePages = false;
curChar = 0;
}
}
public int RemoveZeros(int value)
{
switch (value)
{
case 0:
return 1;
default:
return value;
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
values = new List<double>();
}
void ShowValues()
{
lstResult2.Items.Clear();
for (int i = 0; i < values.Count; i++)
lstResult2.Items.Add(values[i]);
lblSum.Text = values.Count.ToString();
}
private void btnAdd_Click(object sender, EventArgs e)
{
double value = 0.00;
double sum = 0.00;
if (txtItemName.Text == "" || txtItemValue.Text == "")
{
MessageBox.Show("Please Enter A Value In All Fields.", "Missing Information");
return;
}
else
{
lstResult.Items.Add(txtItemName.Text);
txtItemName.Text = "";
try
{
value = double.Parse(txtItemValue.Text);
values.Add(value);
ShowValues();
txtItemValue.Text = "";
txtItemName.Focus();
}
catch (FormatException)
{
}
for (int i = 0; i < values.Count; i++)
sum += values[i];
lblSum.Text = sum.ToString("F");
}
}
private void mnuFileNew_Click(object sender, EventArgs e)
{
txtFormulaName.Text = "";
txtFormulaNumber.Text = "";
txtItemName.Text = "";
comboBox1.Text = "";
lstResult.Items.Clear();
lstResult2.Items.Clear();
}
private void mnuFileOpen_Click(object sender, EventArgs e)
{
dlgOpen.InitialDirectory = @"C:\";
if (dlgOpen.ShowDialog() == DialogResult.OK)
{
FileStream fileAndorHunsFormulaWorksheet = new FileStream(this.dlgOpen.FileName, FileMode.Open, FileAccess.Read, FileShare.Read);
BinaryReader bnrAndorHunsFormulaWorksheet = new BinaryReader(fileAndorHunsFormulaWorksheet);
lblSum.Text = bnrAndorHunsFormulaWorksheet.ReadString();
txtFormulaName.Text = bnrAndorHunsFormulaWorksheet.ReadString();
string item = bnrAndorHunsFormulaWorksheet.ReadString();
int index = comboBox1.FindString(item);
if ((index != -1 & !string.IsNullOrEmpty(item)))
{
comboBox1.SelectedIndex = index;
}
txtFormulaNumber.Text = bnrAndorHunsFormulaWorksheet.ReadString();
int number = lstResult2.Items.Count;
lstResult2.Items.Clear();
lstResult.Items.Clear();
bool switchAction = false;
while (bnrAndorHunsFormulaWorksheet.PeekChar() != -1)
{
string str = bnrAndorHunsFormulaWorksheet.ReadString();
if (str == "@")
{
switchAction = true;
continue;
}
if (switchAction)
lstResult.Items.Add(str);
else
lstResult2.Items.Add(str);
}
bnrAndorHunsFormulaWorksheet.Close();
fileAndorHunsFormulaWorksheet.Close();
}
}
private void mnuFileSave_Click(object sender, EventArgs e)
{
if (dlgSave.ShowDialog() == DialogResult.OK)
{
FileStream fileAndorHunsFormulaWorksheet = new FileStream(dlgSave.FileName, FileMode.Create, FileAccess.Write, FileShare.Write);
BinaryWriter bnrAndorHunsFormulaWorksheet = new BinaryWriter(fileAndorHunsFormulaWorksheet);
bnrAndorHunsFormulaWorksheet.Write(lblSum.Text);
bnrAndorHunsFormulaWorksheet.Write(txtFormulaName.Text);
bnrAndorHunsFormulaWorksheet.Write(comboBox1.Text);
bnrAndorHunsFormulaWorksheet.Write(txtFormulaNumber.Text);
foreach (object ls in lstResult2.Items)
{
bnrAndorHunsFormulaWorksheet.Write(ls.ToString());
}
bnrAndorHunsFormulaWorksheet.Write("@"); // Marker
foreach (object ls in lstResult.Items)
{
bnrAndorHunsFormulaWorksheet.Write(ls.ToString());
}
bnrAndorHunsFormulaWorksheet.Close();
fileAndorHunsFormulaWorksheet.Close();
}
}
private void btnRemoveValue_Click(object sender, EventArgs e)
{
lstResult2.Items.Remove(lstResult2.SelectedItem);
}
private void btnRemoveItem_Click(object sender, EventArgs e)
{
lstResult.Items.Remove(lstResult.SelectedItem);
}
private void mnuFilePrint_Click(object sender, EventArgs e)
{
PCPrint printer = new PCPrint();
printer.PrinterFont = new Font("Verdana", 12);
printer.TextToPrint = txtFormulaName.Text;
printer.TextToPrint = txtFormulaNumber.Text;
printer.TextToPrint = lblItemValue.Text;
foreach (object ls in lstResult.Items)
{
printer.TextToPrint = ls.ToString();
}
printer.TextToPrint = "@";
foreach (object ls in lstResult2.Items)
{
printer.TextToPrint = ls.ToString();
}
printer.Print();
}
}
}