Click here to Skip to main content
15,891,409 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.9K   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 ;
using System.Drawing;

namespace FormPrintComponent
{
    public class TextboxControlPrintSetting: ControlPrintSetting
    {

        #region Private members
        private int _TotalLines = 1;
        private int _LinesPerPage  = 1;
        private int _CurrentLine  = 1;
        private string[] _Lines;

        private const string AverageText = @"ABCDEFGHIJKLMNOPQRSTUVWXYZ";

        #endregion

        /// <summary>
        /// The textbox to print
        /// </summary>
        private TextBox TextboxControl
        {
            get 
            {
                return (TextBox)base.Control;
            }
        }

        /// <summary>
        /// For each new logical page, set the text pointer back to the start
        /// of the control's text
        /// </summary>
        public override void NewLogicalPage()
        {
            base.NewLogicalPage();
            _CurrentLine = 0;
            _Lines = TextboxControl.Lines;
            _TotalLines = _Lines.Length;
        }

        /// <summary>
        /// Prints this textbox on the given page
        /// </summary>
        /// <param name="Canvas"></param>
        public override void PrintOnPage(System.Drawing.Graphics Canvas)
        {
            if (base.DataOverflowAction == DataOverflowActions.TruncateToFit)
            {
                // Print as much of the textbox's text as fits - this is the default behaviour
                // for a ControlPrintSetting
                base.PrintOnPage(Canvas);
            }
            else
            {
                // Print the background for this control
                base.PrintBackground(Canvas );
 
                // Get the height of a line of text....
                Single _lineHeight = Canvas.MeasureString(AverageText, base.PrintFont ).Height ;
                _LinesPerPage = base.BoundingRectangle.Height / (int)_lineHeight ;

                Rectangle _rcnextLine;

                _rcnextLine = new Rectangle(base.BoundingRectangle.Top, base.BoundingRectangle.Left, base.BoundingRectangle.Width, (int)_lineHeight);

                for (int n = _CurrentLine ; n < (_CurrentLine + _LinesPerPage ); n++)
                {
                    if (n > _TotalLines)
                    {
                        break;
                    }
                    else
                    {
                        base.PrintText(Canvas, _Lines[n], _rcnextLine);  
                        _rcnextLine.Offset(0, (int)_lineHeight);
                    }
                }
               // Increment the current line pointer
                _CurrentLine += _LinesPerPage;

                // Print the borders
                base.PrintBorders(Canvas); 

            }
        }

        /// <summary>
        /// Returns true if the data in this control needs more pages
        /// </summary>
        /// <remarks>
        /// For a multi line text box this is true if the current line is less than the total lines
        /// per page
        /// </remarks>
        public override bool HasMorePages
        {
            get
            {
                if (base.DataOverflowAction == DataOverflowActions.TruncateToFit)
                {
                    return false;
                }
                else
                {
                    if (_CurrentLine < _TotalLines)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
        }

        #region Public constructor
        public TextboxControlPrintSetting(TextBox textboxToPrint) : base(textboxToPrint )
        {
            // Initailise the textbox pointer to the start of the text
            this.NewLogicalPage();
        }
        #endregion

    }
}

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