|
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.