Click here to Skip to main content
15,886,067 members
Articles / Web Development / HTML

Form Print Control

Rate me:
Please Sign up or sign in to vote.
3.71/5 (7 votes)
21 Jan 2009CPOL7 min read 264.4K   7.3K   112  
An extender control to make printing a form quick, easy, and flexible
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
 
namespace FormPrintComponent
{
    /// <summary>
    /// A collection of print settings for all the controls on a form, for example
    /// </summary>
    public class ControlPrintSettings: Dictionary<Control, ControlPrintSetting >
    {

        #region private members
        private System.Drawing.Printing.PaperKind _PaperKind ;
        private uint _LogicalPageCount = 1;
        #endregion

        /// <summary>
        /// The number of logical pages that this control print settings 
        /// collection is being printed for
        /// </summary>
        /// <remarks >
        /// A logical page is like a template from which one or more physical
        /// pages will be printed depending on how much data
        /// </remarks>
        public uint LogicalPageCount
        {
            get 
            { 
                return _LogicalPageCount; 
            }
            set 
            { 
                _LogicalPageCount = value;
                foreach (ControlPrintSetting cpl in this.Values )
                {
                    cpl.LogicalPageCount = _LogicalPageCount;
                }
            }
        }

        /// <summary>
        /// Collection indexer
        /// </summary>
        /// <param name="indexer">
        /// The control to get the print setting(s) for
        /// </param>
        /// <returns>
        /// </returns>
        public new ControlPrintSetting this[Control indexer]
        {
            get 
            {
              // If this control is not already in the collection, add it
            if (! base.ContainsKey(indexer ))
            {
                if ( indexer is RichTextBox)
                {
                    this.Add(indexer , new RTFControlPrintSetting((RichTextBox)indexer));  
                }
                else
                {
                    if (indexer is TextBox )
                    {
                        this.Add(indexer , new TextboxControlPrintSetting((TextBox)indexer)); 
                    }
                    else
                    {
                        this.Add(indexer , new ControlPrintSetting(indexer));
                    }
                }
            }
                return base[indexer];
            }
            set { base[indexer] = value; }
        }

        /// <summary>
        /// Returns a collection of controls that should be printed on this 
        /// logical page number
        /// </summary>
        /// <param name="LogicalpageNumber">
        /// The logical page number to print
        /// </param>
        /// <returns>
        /// </returns>
        public List<ControlPrintSetting> ControlsToPrintForLogicalPage(int LogicalpageNumber)
        {
            List<ControlPrintSetting> _ret = new List<ControlPrintSetting>();

            foreach (ControlPrintSetting cpl in this.Values )
            {
                switch (cpl.MultiPagePrintMethod)
                {
                    case ControlPrintSetting.MultiPagePrintMethods.PrintOnEveryPage:
                        {
                            _ret.Add(cpl);
                            break;
                        }
                    case ControlPrintSetting.MultiPagePrintMethods.PrintOnOddPages:
                        {
                            if (LogicalpageNumber % 2 == 1)
                            {
                                _ret.Add(cpl);
                            }
                            break;
                        }
                    case ControlPrintSetting.MultiPagePrintMethods.PrintOnEvenPages:
                        {
                            if (LogicalpageNumber % 2 == 0)
                            {
                                _ret.Add(cpl);
                            }
                            break;
                        }
                    case ControlPrintSetting.MultiPagePrintMethods.PrintOnlyOnSpecifiedLogicalPages:
                        {
                            if (cpl.PrintOnPages[LogicalpageNumber-1])
                            {
                                _ret.Add(cpl); 
                            }
                            break;
                        }
                }
            } 

            return _ret;
        }
    
    }
}

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
Software Developer
Ireland Ireland
C# / SQL Server developer
Microsoft MVP (Azure) 2017
Microsoft MVP (Visual Basic) 2006, 2007

Comments and Discussions