Click here to Skip to main content
15,891,529 members
Articles / Programming Languages / Visual Basic

Printing Reports in .NET

Rate me:
Please Sign up or sign in to vote.
4.85/5 (70 votes)
26 Aug 2008CPOL11 min read 440.6K   15.6K   257  
Using the library presented, you can print reports from C# and other .NET languages
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Printing;
using System.Windows.Forms;
using System.Collections;
using System.Data;

namespace ReportDocumentTesting
{

    #region "Public enums"
    /// <summary>
    /// The types of printouts that can be made
    /// </summary>
    public enum PrintWhat
    { 
        /// <summary>
        /// Prints a roster showing names of students
        /// </summary>
        RosterWithNames, 
        /// <summary>
        /// Prints a roster showing codes of students
        /// </summary>
        RosterWithCodes, 
        /// <summary>
        /// Prints individual pages for each student
        /// </summary>
        IndividualPages };

    /// <summary>
    /// Things to order by
    /// </summary>
    public enum PrintOrderBy
    {
        /// <summary>
        /// Prints the students alphabetically by last name, first name
        /// </summary>
        Name,
        /// <summary>
        /// Prints the students alphabetically by code name
        /// </summary>
        CodeName,
        /// <summary>
        /// Prints the students arrange by average, highest first.
        /// </summary>
        Average
    }
    #endregion


	/// <summary>
	/// 
	/// </summary>
	public class My1PrintDialog : Form //CommonDialog
	{
        

        #region "Windows Designer Generated Code"

        private System.Windows.Forms.PageSetupDialog pageSetupDialog1;
        private System.Windows.Forms.Button btnCancel;
        private System.Windows.Forms.Button btnPreview;
        private System.Windows.Forms.ComboBox cmbPrintWhat;
        private System.Windows.Forms.Label lblPrintWhat;
        private System.Windows.Forms.Label lblOrder;
        private System.Windows.Forms.ComboBox cmbOrderBy;
        private System.Windows.Forms.GroupBox studentsGroupBox;
        private System.Windows.Forms.ListBox listStudents;
        private System.Windows.Forms.Button btnPageSetup;
        private System.Windows.Forms.Button btnOK;


        private void InitializeComponent()
        {
            this.pageSetupDialog1 = new System.Windows.Forms.PageSetupDialog();
            this.btnOK = new System.Windows.Forms.Button();
            this.btnCancel = new System.Windows.Forms.Button();
            this.btnPreview = new System.Windows.Forms.Button();
            this.cmbPrintWhat = new System.Windows.Forms.ComboBox();
            this.lblPrintWhat = new System.Windows.Forms.Label();
            this.lblOrder = new System.Windows.Forms.Label();
            this.cmbOrderBy = new System.Windows.Forms.ComboBox();
            this.studentsGroupBox = new System.Windows.Forms.GroupBox();
            this.listStudents = new System.Windows.Forms.ListBox();
            this.btnPageSetup = new System.Windows.Forms.Button();
            this.studentsGroupBox.SuspendLayout();
            this.SuspendLayout();
            // 
            // btnOK
            // 
            this.btnOK.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right);
            this.btnOK.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.btnOK.Location = new System.Drawing.Point(336, 328);
            this.btnOK.Name = "btnOK";
            this.btnOK.TabIndex = 1;
            this.btnOK.Text = "&OK";
            this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
            // 
            // btnCancel
            // 
            this.btnCancel.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right);
            this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.btnCancel.Location = new System.Drawing.Point(429, 328);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.TabIndex = 2;
            this.btnCancel.Text = "&Cancel";
            // 
            // btnPreview
            // 
            this.btnPreview.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right);
            this.btnPreview.Location = new System.Drawing.Point(240, 328);
            this.btnPreview.Name = "btnPreview";
            this.btnPreview.TabIndex = 4;
            this.btnPreview.Text = "Pre&view";
            this.btnPreview.Click += new System.EventHandler(this.btnPreview_Click);
            // 
            // cmbPrintWhat
            // 
            this.cmbPrintWhat.Location = new System.Drawing.Point(80, 187);
            this.cmbPrintWhat.Name = "cmbPrintWhat";
            this.cmbPrintWhat.Size = new System.Drawing.Size(152, 21);
            this.cmbPrintWhat.TabIndex = 5;
            this.cmbPrintWhat.SelectedIndexChanged += new System.EventHandler(this.cmbPrintWhat_SelectedIndexChanged);
            // 
            // lblPrintWhat
            // 
            this.lblPrintWhat.Location = new System.Drawing.Point(16, 192);
            this.lblPrintWhat.Name = "lblPrintWhat";
            this.lblPrintWhat.Size = new System.Drawing.Size(64, 16);
            this.lblPrintWhat.TabIndex = 6;
            this.lblPrintWhat.Text = "Print &what:";
            this.lblPrintWhat.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // lblOrder
            // 
            this.lblOrder.Location = new System.Drawing.Point(16, 232);
            this.lblOrder.Name = "lblOrder";
            this.lblOrder.Size = new System.Drawing.Size(56, 16);
            this.lblOrder.TabIndex = 8;
            this.lblOrder.Text = "&Order by:";
            this.lblOrder.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // cmbOrderBy
            // 
            this.cmbOrderBy.Location = new System.Drawing.Point(80, 224);
            this.cmbOrderBy.Name = "cmbOrderBy";
            this.cmbOrderBy.Size = new System.Drawing.Size(152, 21);
            this.cmbOrderBy.TabIndex = 7;
            // 
            // studentsGroupBox
            // 
            this.studentsGroupBox.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                                           this.listStudents});
            this.studentsGroupBox.Location = new System.Drawing.Point(248, 176);
            this.studentsGroupBox.Name = "studentsGroupBox";
            this.studentsGroupBox.Size = new System.Drawing.Size(256, 136);
            this.studentsGroupBox.TabIndex = 9;
            this.studentsGroupBox.TabStop = false;
            this.studentsGroupBox.Text = "Choose students";
            // 
            // listStudents
            // 
            this.listStudents.Location = new System.Drawing.Point(16, 24);
            this.listStudents.Name = "listStudents";
            this.listStudents.ScrollAlwaysVisible = true;
            this.listStudents.SelectionMode = System.Windows.Forms.SelectionMode.MultiExtended;
            this.listStudents.Size = new System.Drawing.Size(224, 95);
            this.listStudents.TabIndex = 0;
            // 
            // btnPageSetup
            // 
            this.btnPageSetup.Location = new System.Drawing.Point(128, 328);
            this.btnPageSetup.Name = "btnPageSetup";
            this.btnPageSetup.Size = new System.Drawing.Size(96, 23);
            this.btnPageSetup.TabIndex = 6;
            this.btnPageSetup.Text = "Page &Setup";
            this.btnPageSetup.Click += new System.EventHandler(this.btnPageSetup_Click);
            // 
            // BasicPrintDialog
            // 
            this.AcceptButton = this.btnOK;
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.CancelButton = this.btnCancel;
            this.ClientSize = new System.Drawing.Size(520, 367);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.studentsGroupBox,
                                                                          this.lblOrder,
                                                                          this.cmbOrderBy,
                                                                          this.lblPrintWhat,
                                                                          this.cmbPrintWhat,
                                                                          this.btnPreview,
                                                                          this.btnCancel,
                                                                          this.btnOK,
                                                                          this.btnPageSetup});
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "BasicPrintDialog";
            this.Text = "Print";
            this.Load += new System.EventHandler(this.BasicPrintDialog_Load);
            this.studentsGroupBox.ResumeLayout(false);
            this.ResumeLayout(false);

        }


        #endregion

        #region "Combo List Helpers"
        /// <summary>
        /// A list of ComboObjects that fills the PrintWhat combo list
        /// </summary>
        private ArrayList printWhatList;
        /// <summary>
        /// A list of ComboObjects that fills the OrderBy combo list
        /// </summary>
        private ArrayList printOrderByList;

        #endregion

        
        /// <summary>
        /// Constructor
        /// </summary>
        public My1PrintDialog()
        {
            this.InitializeComponent();

        }

        /// <summary>
        /// Handle the form loading
        /// </summary>
        private void BasicPrintDialog_Load(object sender, System.EventArgs e)
        {

            this.printWhatList = new ArrayList();
            this.printWhatList.Add(new ComboObject("Roster showing names", PrintWhat.RosterWithNames));
            this.printWhatList.Add(new ComboObject("Roster showing codes", PrintWhat.RosterWithCodes));
            this.printWhatList.Add(new ComboObject("Individual student reports", PrintWhat.IndividualPages));

            this.printOrderByList = new ArrayList();
            this.printOrderByList.Add(new ComboObject("Name (Last, First)", PrintOrderBy.Name));
            this.printOrderByList.Add(new ComboObject("CodeName", PrintOrderBy.CodeName));
            this.printOrderByList.Add(new ComboObject("Average", PrintOrderBy.Average));


            // Must set value/display member first, then set the datasource.
            this.cmbPrintWhat.ValueMember = "Value";
            this.cmbPrintWhat.DisplayMember = "Display";
            this.cmbPrintWhat.DataSource = this.printWhatList;

            this.cmbOrderBy.ValueMember = "Value";
            this.cmbOrderBy.DisplayMember = "Display";
            this.cmbOrderBy.DataSource = this.printOrderByList;
            


            // Select all students
//            foreach (ListItem li in this.listStudents.Items)
        }

        #region "Locals and Properties"

        /// <summary>
        /// The document used for printing
        /// </summary>
        private ReportDocument printDocument;

        /// <summary>
        /// Gets or sets the PrintDocument used by the dialog.
        /// </summary>
        public ReportDocument Document
        {
            get { return this.printDocument; }
            set { this.printDocument = value; }
        }

        /// <summary>
        /// A DataTable of the students used for the Choose students drop-down list.
        /// </summary>
        private DataTable studentsTable;

        public DataTable StudentsTable
        {
            get { return this.studentsTable; }
            set 
            { 
                this.studentsTable = value;
                this.listStudents.DataSource = this.studentsTable;
                this.listStudents.DisplayMember = "Name";
                this.listStudents.ValueMember = "Id";
            }
        }

        #endregion


        #region "Form Handlers"



        private void btnPageSetup_Click(object sender, System.EventArgs e)
        {
            this.pageSetupDialog1.Document = this.printDocument;
            this.pageSetupDialog1.ShowDialog(this);
        }



        private void btnPreview_Click(object sender, System.EventArgs e)
        {
            PrintPreviewDialog ppd = new PrintPreviewDialog();
            ppd.Document = this.printDocument;
            ppd.Height = (int) (this.Owner.Height * 0.8F);
            ppd.Width = (int) (ppd.Height);

            ppd.ShowDialog(this);
        }

        private void SaveSettings()
        {
            // TODO: save the printWhat and list of students to document
        }

        private void btnOK_Click(object sender, System.EventArgs e)
        {
            SaveSettings();
        }

        #endregion

        private void cmbPrintWhat_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            // Check that the selected value is not null, and that it is not a ComboObject
            if ( (this.cmbPrintWhat.SelectedValue != null) )
                //&& (!(this.cmbPrintWhat.SelectedValue is ComboObject)) )
            {
                switch ((PrintWhat) this.cmbPrintWhat.SelectedValue)
                {
                    case PrintWhat.IndividualPages:
                        // enable the choose students list
                        this.studentsGroupBox.Enabled = true;
                        foreach (Control control in this.studentsGroupBox.Controls)
                        {
                            control.Enabled = true;
                        }
                        // disable the OrderBy combo box
                        this.lblOrder.Enabled = false;
                        this.cmbOrderBy.Enabled = false;
                        break;

                    default:
                        // disable the choose students list
                        this.studentsGroupBox.Enabled = false;
                        foreach (Control control in this.studentsGroupBox.Controls)
                        {
                            control.Enabled = false;
                        }
                        // enable the OrderBy combo box
                        this.lblOrder.Enabled = true;
                        this.cmbOrderBy.Enabled = true;

                        break;
                } // switch
            }
        } // cmbPrintWhat_SelectedIndexChanged


	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions