# Printing using c#



## Lewisharvey (Apr 12, 2010)

Hello,

I am currently working on my exam program and I am trying to enable the print button so I can print my test scores but somehow its not working.I have the coding for the print button but don't know how to input it properly so that the print button will work and let me print the scores.

I hope someone can help me with this.

Here is the code I got for my program:

```
namespace Quiz{

public partial class Form1 : Form
{
int score;
int firstname;

//Number of seconds students have to complete the task.
double tick_count = 1800;
//Redesign Question 10
//Redesign Question 3
//Have more textbox questions
//Timer needs setting up
//Database needs setting up

public Form1()
{

InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)
{
timer.Start();
tabControl1.SelectedTab = tabPage2;
}

private void button2_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage3;
}

private void button3_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage4;
}

private void pictureBox3_Click(object sender, EventArgs e)
{
//Does not work
}

private void pictureBox4_Click(object sender, EventArgs e)
{
//Does not work
}

private void pictureBox5_Click(object sender, EventArgs e)
{
//Does not work
}

private void pictureBox6_Click(object sender, EventArgs e)
{
//Does not work
}

private void button4_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage5;
}

private void Form1_Load(object sender, EventArgs e)
{
//Do Not Need
}

private void button5_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage6;
}

private void button6_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage7;
}

private void button7_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage8;
}

private void button8_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage9;
}

private void button9_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage10;
}

private void button10_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage11;
}

private void button11_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage12;
}

private void button12_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage13;
}

private void button13_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage14;

}

private void button14_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage15;

}

private void button15_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage16;

}

private void button16_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage17;

}

private void button17_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage18;

}

private void button18_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage19;

}

private void button19_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage20;

}

private void button20_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage21;
}

private void Generate_Score()
{
score = 0;
if (radioButton2.Checked) score++;
if (radioButton16.Checked) score++;
if (checkBox54.Checked) score++;
if (checkBox56.Checked) score++;
if (checkBox57.Checked) score++;
if (checkBox1.Checked) score++;
if (checkBox5.Checked) score++;
if (checkBox9.Checked) score++;
if (checkBox11.Checked) score++;
if (checkBox13.Checked) score++;
if (radioButton9.Checked) score++;
if (radioButton7.Checked) score++;
if (radioButton19.Checked) score++;
if (radioButton22.Checked) score++;
if (checkBox46.Checked) score++;
if (checkBox52.Checked) score++;
if (radioButton53.Checked) score++;
if (radioButton27.Checked) score++;
if (radioButton32.Checked) score++;
if (radioButton35.Checked) score++;
if (radioButton37.Checked) score++;
if (radioButton39.Checked) score++;
if (radioButton45.Checked) score++;
if (radioButton48.Checked) score++;
if (radioButton50.Checked) score++;
textBox2.Text = score.ToString();
timer.Stop();
tabControl1.SelectedTab = tabPage22;
}

private void button22_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage2;
}

private void button23_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage3;
}

private void button24_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage4;
}

private void button42_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage20;
}

private void button41_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage19;
}

private void button40_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage18;
}

private void button39_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage17;
}

private void button38_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage16;
}

private void button37_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage15;
}

private void button36_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage14;
}

private void button35_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage13;
}

private void button34_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage12;
}

private void button33_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage11;
}

private void button32_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage10;
}

private void button31_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage9;
}

private void button30_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage8;
}

private void button29_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage7;
}

private void button28_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage6;
}

private void button27_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage5;
}

private void timer1_Tick(object sender, EventArgs e)
{
//Do Not Need
}

private void timer1_Tick_1(object sender, EventArgs e)
{
//Do Not Need
}

private void label38_Click(object sender, EventArgs e)
{
//Do Not Need
}

private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)
{
//Do Not Need
}

private void label61_Click(object sender, EventArgs e)
{
//Do Not Need

}

private void button43_Click(object sender, EventArgs e)
{
//Do Not Need
}

private void tabPage8_Click(object sender, EventArgs e)
{
//Do Not Need
}

private void textBox17_TextChanged(object sender, EventArgs e)
{
//Do Not Need
}

private void tabPage14_Click(object sender, EventArgs e)
{
//Do Not Need
}

private void tabPage16_Click(object sender, EventArgs e)
{
//Do Not Need
}

private void tabPage18_Click(object sender, EventArgs e)
{
//Do Not Need
}

private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
//Do Not Need
}

private void button43_Click_1(object sender, EventArgs e)
{
Generate_Score();
}

private void textBox9_TextChanged(object sender, EventArgs e)
{

}

private void timer1_Tick_2(object sender, EventArgs e)
{
tick_count--;

if (tick_count == 0)
{
foreach (TabPage page in tabControl1.TabPages)
{
if (page != tabPage1 && page != tabPage22)
{
foreach (Control innerControl in page.Controls)
{
innerControl.Enabled = false;
}
}
}

tabControl1.SelectedIndex = 21;
Generate_Score();
// Time's up! Want help writing this bit? Tell me what you want to happen when the time runs out.
}

TimeSpan t = TimeSpan.FromSeconds(tick_count);
label38.Text = string.Format("Time remaining: {0:D2}h:{1:D2}m:{2:D2}s", t.Hours, t.Minutes, t.Seconds);

}

private void label38_Click_1(object sender, EventArgs e)
{
//Not in use
}

private void tabPage22_Click(object sender, EventArgs e)
{
//Not in use
}

private void label96_Click(object sender, EventArgs e)
{
//Not in use
}

private void label8_Click(object sender, EventArgs e)
{

}

private void button21_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage22;
}

private void button22_Click_1(object sender, EventArgs e)
{
Close();
}

}
}
```
Here is the coding for the print button I got:

```
{
//Create an instance of our printer class
PCPrint printer = new PCPrint();
//Set the font we want to use
printer.PrinterFont = new Font("Verdana", 10);
//Set the TextToPrint property
printer.TextToPrint = " Hello world \n\n";
//Now loop throught the items in the list and print each
for (int i = 0; i < listBox1.Items.Count; i++)
{
printer.TextToPrint += listBox1.Items + "\n";
}
//Issue print command
printer.Print();
}
}
}
```
Thank you


----------



## TLF_KoBE (Jan 23, 2012)

The code that print button came from is here:
Printing In C# - C# Tutorials | Dream.In.Code

You are missing the PCPrint() Class


```
//*****************************************************************************************
//                           LICENSE INFORMATION
//*****************************************************************************************
//   PCPrint Version 1.0.0.0
//   Class file for printing in VB.Net. Inherits from the PrintDocument Class, and includes
//   all its functionality
//
//   Copyright (C) 2008  
//   Richard L. McCutchen 
//   Email: [email protected]
//   Created: 25FEB08
//
//   This program is free software: you can redistribute it and/or modify
//   it under the terms of the GNU General Public License as published by
//   the Free Software Foundation, either version 3 of the License, or
//   (at your option) any later version.
//
//   This program is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   GNU General Public License for more details.
//
//   You should have received a copy of the GNU General Public License
//   along with this program.  If not, see <http://www.gnu.org/licenses/>.
//*****************************************************************************************
using System;
using System.Drawing;
using System.Drawing.Printing;


namespace PC
{
    public class PCPrint : System.Drawing.Printing.PrintDocument
    {
        #region  Property Variables 
        /// <summary>
        /// Property variable for the Font the user wishes to use
        /// </summary>
        /// <remarks></remarks>
        private Font _font;

        /// <summary>
        /// Property variable for the text to be printed
        /// </summary>
        /// <remarks></remarks>
        private string _text;
        #endregion

        #region Static Local Variables
        /// <summary>
        /// Static variable to hold the current character
        /// we're currently dealing with.
        /// </summary>
        static int curChar;
        #endregion

        #region  Class Properties
        /// <summary>
        /// Property to hold the text that is to be printed
        /// </summary>
        /// <value></value>
        /// <returns>A string</returns>
        /// <remarks></remarks>
        public string TextToPrint
        {
            get { return _text; }
            set { _text = value; }
        }

        /// <summary>
        /// Property to hold the font the users wishes to use
        /// </summary>
        /// <value></value>
        /// <returns></returns>
        /// <remarks></remarks>
        public Font PrinterFont
        {
            // Allows the user to override the default font
            get { return _font; }
            set { _font = value; }
        }
        #endregion

        #region  Class Constructors 
        /// <summary>
        /// Empty constructor
        /// </summary>
        /// <remarks></remarks>
        public PCPrint() : base()
        {
            //Set the file stream
            //Instantiate out Text property to an empty string
            _text = string.Empty;
        }

        /// <summary>
        /// Constructor to initialize our printing object
        /// and the text it's supposed to be printing
        /// </summary>
        /// <param name=str>Text that will be printed</param>
        /// <remarks></remarks>
        public PCPrint(string str) : base()
        {
            //Set the file stream
            //Set our Text property value
            _text = str;
        }
        #endregion

        #region  OnBeginPrint 
        /// <summary>
        /// Override the default OnBeginPrint method of the PrintDocument Object
        /// </summary>
        /// <param name=e></param>
        /// <remarks></remarks>
        protected override void OnBeginPrint(System.Drawing.Printing.PrintEventArgs e)
        {
            // Run base code
            base.OnBeginPrint(e);

            //Check to see if the user provided a font
            //if they didnt then we default to Times New Roman
            if (_font == null)
            {
                //Create the font we need
                _font = new Font("Times New Roman", 10);
            }
        }
        #endregion

        #region  OnPrintPage 
        /// <summary>
        /// Override the default OnPrintPage method of the PrintDocument
        /// </summary>
        /// <param name=e></param>
        /// <remarks>This provides the print logic for our document</remarks>
        protected override void OnPrintPage(System.Drawing.Printing.PrintPageEventArgs e)
        {
            // Run base code
            base.OnPrintPage(e);

            //Declare local variables needed
            
            int printHeight;
            int printWidth;
            int leftMargin;
            int rightMargin;
            Int32 lines;
            Int32 chars;

            //Set print area size and margins
            {
                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;
                //X
                rightMargin = base.DefaultPageSettings.Margins.Top;
                //Y
            }

            //Check if the user selected to print in Landscape mode
            //if they did then we need to swap height/width parameters
            if (base.DefaultPageSettings.Landscape)
            {
                int tmp;
                tmp = printHeight;
                printHeight = printWidth;
                printWidth = tmp;
            }

            //Now we need to determine the total number of lines
            //we're going to be printing
            Int32 numLines = (int)printHeight / PrinterFont.Height;

            //Create a rectangle printing are for our document
            RectangleF printArea = new RectangleF(leftMargin, rightMargin, printWidth, printHeight);

            //Use the StringFormat class for the text layout of our document
            StringFormat format = new StringFormat(StringFormatFlags.LineLimit);

            //Fit as many characters as we can into the print area      

            e.Graphics.MeasureString(_text.Substring(RemoveZeros(curChar)), PrinterFont, new SizeF(printWidth, printHeight), format, out chars, out lines);

            //Print the page
            e.Graphics.DrawString(_text.Substring(RemoveZeros(curChar)), PrinterFont, Brushes.Black, printArea, format);

            //Increase current char count
            curChar += chars;

            //Detemine if there is more text to print, if
            //there is the tell the printer there is more coming
            if (curChar < _text.Length)
            {
                e.HasMorePages = true;
            }
            else
            {
                e.HasMorePages = false;
                curChar = 0;
            }
        }

        #endregion

        #region  RemoveZeros 
        /// <summary>
        /// Function to replace any zeros in the size to a 1
        /// Zero's will mess up the printing area
        /// </summary>
        /// <param name=value>Value to check</param>
        /// <returns></returns>
        /// <remarks></remarks>
        public int RemoveZeros(int value)
        {
            //Check the value passed into the function,
            //if the value is a 0 (zero) then return a 1,
            //otherwise return the value passed in
            switch (value)
            {
                case 0:
                    return 1;
                default:
                    return value;
            }
        }
        #endregion
    }

}
```


----------



## Lewisharvey (Apr 12, 2010)

Ok thanks but when I print it prints two pages but it only needs to be 1


----------

