📄 textfield.cs
字号:
#region License
/*
This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General
Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option)
any later version.
This library 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 Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to
the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#endregion
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
namespace daReport
{
/// <summary>
/// Class representing the TextField object.
/// </summary>
/// <remarks>The TextField is used in reports where graphical representation of data is used.</remarks>
public class TextField : ICustomPaint
{
#region Declarations
private string mText = "Sample text";
private Font mFont = new Font("Tahoma",10);
private Color mForegroundColor = Color.Black;
private Color mBackgroundColor = Color.Transparent;
private Color mBorderColor = Color.Black;
private int mBorderWidth = 0;
private int mInnerPadding = 2;
private int mLineSpacing = 3;
private bool mSelectable = true;
/// <summary>
/// Enumeration of possible horizontal alignments for the TextField text
/// </summary>
public enum TextAlignmentType
{
/// <summary>Text is aligned to the left of the TextField</summary>
Left = 1,
/// <summary>Text is aligned to the center of the TextField</summary>
Center,
/// <summary>Text is aligned to the right of the TextField</summary>
Right,
/// <summary>Text is aligned to both sides of the TextField</summary>
Justified,
/// <summary>The text is not aligned and will be displayed normally</summary>
None
};
/// <summary>
/// Enumeration of possible horizontal alignments for the TextField text
/// </summary>
public enum TextVerticalAlignmentType
{
/// <summary>Text is aligned to the top of the TextField</summary>
Top = 1,
/// <summary>Text is aligned to the middle of the TextField</summary>
Middle,
/// <summary>Text is aligned to the bottom of the TextField</summary>
Bottom,
/// <summary>The text is not aligned and will be displayed normally</summary>
None
};
private TextAlignmentType mTextAlignment = TextAlignmentType.Left;
private TextVerticalAlignmentType mTextVerticalAlignment = TextVerticalAlignmentType.Top;
private Rectangle mRegion = new Rectangle(0,0,0,0);
private ArrayList mLines;
#endregion
#region Public Properties
/// <summary>
/// Gets/Sets the background color for the TextField
/// </summary>
/// <remarks>This property sets the background color of the TextField object. This can be any color
/// from the System.Drawing.Color structure
/// </remarks>
public Color BackgroundColor
{
get {return mBackgroundColor;}
set {mBackgroundColor = value;}
}
/// <summary>
/// Gets/Sets the border color for the TextField
/// </summary>
/// <remarks>This property sets the border color of the TextField object. This can be any color
/// from the System.Drawing.Color structure
/// </remarks>
public Color BorderColor
{
get {return mBorderColor;}
set {mBorderColor = value;}
}
/// <summary>
/// Gets/Sets the border width for the TextField
/// </summary>
/// <remarks>
/// BorderWidth of the TextField. If this is set to zero, then the border is invisible
/// </remarks>
public int BorderWidth
{
get {return mBorderWidth;}
set {mBorderWidth = value;}
}
/// <summary>
/// Gets/Sets the font used in the TextField object
/// </summary>
public Font Font
{
get {return mFont;}
set {mFont = value;}
}
/// <summary>
/// Gets/Sets the foreground color for the TextField
/// </summary>
/// <remarks>This property sets the foreground color of the TextField object. This can be any color
/// from the System.Drawing.Color structure
/// </remarks>
public Color ForegroundColor
{
get {return mForegroundColor;}
set {mForegroundColor = value;}
}
/// <summary>
/// Gets/Sets text to be displayed in the TextField object.
/// </summary>
[Editor(typeof(Editors.PlainTextEditor), typeof(UITypeEditor)), Description("Text content of the Text Field.")]
public string Text
{
get {return mText;}
set {mText = value;}
}
/// <summary>
/// Gets or sets the horizontal alignment of text in the ChartBox
/// </summary>
[Description("Horizontal alignment of text in the TextField object, relative to borders.")]
public TextAlignmentType TextAlignment
{
get {return mTextAlignment;}
set {mTextAlignment = value;}
}
/// <summary>
/// Gets or sets the vertical alignment of text in the ChartBox
/// </summary>
[Description("Horizontal vertical of text in the TextField object, relative to borders.")]
public TextVerticalAlignmentType TextVerticalAlignment
{
get {return mTextVerticalAlignment;}
set {mTextVerticalAlignment = value;}
}
#endregion
#region Public Overrides
/// <summary>
/// This function adds the current objects information to the XmlTextWriter
/// </summary>
/// <param name="CurrentWriter">Current XmlTextWriter to append object information to</param>
public override void AddXMLToWriter(ref System.Xml.XmlTextWriter CurrentWriter)
{
CurrentWriter.WriteStartElement("textField");
CurrentWriter.WriteAttributeString("x", this.X.ToString());
CurrentWriter.WriteAttributeString("y", this.Y.ToString());
CurrentWriter.WriteAttributeString("width", this.Width.ToString());
CurrentWriter.WriteAttributeString("height", this.Height.ToString());
CurrentWriter.WriteAttributeString("horAlignment", this.horizontalAlignment.ToString());
CurrentWriter.WriteAttributeString("verAlignment", this.verticalAlignment.ToString());
CurrentWriter.WriteAttributeString("Selectable", this.mSelectable.ToString());
CurrentWriter.WriteAttributeString("ObjectRepeatability", this.objectRepeatability.ToString());
CurrentWriter.WriteStartElement("text");
CurrentWriter.WriteAttributeString("horAlignment", this.mTextAlignment.ToString());
CurrentWriter.WriteAttributeString("verAlignment", this.mTextVerticalAlignment.ToString());
CurrentWriter.WriteString(Helpers.TextSafeForXML(this.mText));
CurrentWriter.WriteEndElement(); //end of text element
CurrentWriter.WriteStartElement("font");
CurrentWriter.WriteAttributeString("family", this.mFont.FontFamily.Name);
CurrentWriter.WriteAttributeString("size", this.mFont.Size.ToString());
CurrentWriter.WriteAttributeString("style", this.mFont.Style.ToString());
CurrentWriter.WriteEndElement(); //end of font element
CurrentWriter.WriteStartElement("foregroundColor");
CurrentWriter.WriteAttributeString("color", this.mForegroundColor.Name);
CurrentWriter.WriteEndElement(); //end of foregroundColor element
CurrentWriter.WriteStartElement("backgroundColor");
CurrentWriter.WriteAttributeString("color", this.mBackgroundColor.Name);
CurrentWriter.WriteEndElement(); //end of backgroundColor element
CurrentWriter.WriteStartElement("border");
CurrentWriter.WriteAttributeString("width", this.mBorderWidth.ToString());
CurrentWriter.WriteAttributeString("color", this.mBorderColor.Name);
CurrentWriter.WriteEndElement(); //end of border element
CurrentWriter.WriteEndElement(); //end of textBox element
}
/// <summary>
/// Loads the current objects information from the XmlNode passed
/// </summary>
/// <param name="CurrentNode">XmlNode with objects information</param>
public override void LoadFromXMLNode(System.Xml.XmlNode CurrentNode)
{
this.X = Convert.ToInt32( CurrentNode.Attributes["x"].Value );
this.Y = Convert.ToInt32( CurrentNode.Attributes["y"].Value );
this.Width= Convert.ToInt32( CurrentNode.Attributes["width"].Value );
this.Height = Convert.ToInt32( CurrentNode.Attributes["height"].Value );
if (CurrentNode.Attributes["horAlignment"] != null)
this.horizontalAlignment = ICustomPaint.ResolveHorizontalAlignment(CurrentNode.Attributes["horAlignment"].Value);
if (CurrentNode.Attributes["verAlignment"] != null)
this.verticalAlignment = ICustomPaint.ResolveVerticalAlignment(CurrentNode.Attributes["verAlignment"].Value);
if (CurrentNode.Attributes["Selectable"] != null)
this.mSelectable = Convert.ToBoolean(CurrentNode.Attributes["Selectable"].Value);
if (CurrentNode.Attributes["ObjectRepeatability"] != null)
this.objectRepeatability = ICustomPaint.ResolveObjectRepeatability(CurrentNode.Attributes["ObjectRepeatability"].Value);
System.Xml.XmlNodeList childNodes = CurrentNode.ChildNodes;
for (int i=0;i<childNodes.Count;i++)
{
switch (childNodes[i].Name)
{
case "text":
this.mText = Helpers.SafeXMLToText(childNodes[i].InnerText);
string alignmentType = childNodes[i].Attributes["horAlignment"]==null ? "Left" : childNodes[i].Attributes["horAlignment"].Value;
switch (alignmentType)
{
case "Center":
this.mTextAlignment = TextField.TextAlignmentType.Center;
break;
case "Right":
this.mTextAlignment = TextField.TextAlignmentType.Right;
break;
case "Justified":
this.mTextAlignment = TextField.TextAlignmentType.Justified;
break;
default :
this.mTextAlignment = TextField.TextAlignmentType.Left;
break;
}
if ( childNodes[i].Attributes["verAlignment"]!= null )
{
switch (childNodes[i].Attributes["verAlignment"].Value)
{
case "Middle":
this.mTextVerticalAlignment = TextField.TextVerticalAlignmentType.Middle;
break;
case "Bottom":
this.mTextVerticalAlignment = TextField.TextVerticalAlignmentType.Bottom;
break;
}
}
break;
case "font":
this.mFont = Helpers.ResolveFont( childNodes[i] );
break;
case "foregroundColor":
try
{
this.mForegroundColor = Color.FromName( childNodes[i].Attributes["color"].Value );
}
catch (Exception){}
break;
case "backgroundColor":
try
{
this.mBackgroundColor = Color.FromName( childNodes[i].Attributes["color"].Value );
}
catch (Exception){}
break;
case "border":
try
{
this.mBorderColor = Color.FromName( childNodes[i].Attributes["color"].Value );
this.mBorderWidth = childNodes[i].Attributes["width"]==null ? 0 : Convert.ToInt32( childNodes[i].Attributes["width"].Value );
}
catch (Exception){}
break;
}
}
}
/// <summary>
/// Gets or sets the horizontal alignment of the TextField
/// </summary>
[Description("Horizontal alignment in the page, relative to margins. This property overrides element coordinates.")]
public override ICustomPaint.HorizontalAlignmentTypes HorizontalAlignment
{
get {return this.horizontalAlignment;}
set
{
this.horizontalAlignment = value;
switch (this.horizontalAlignment)
{
case ICustomPaint.HorizontalAlignmentTypes.Center:
this.mRegion.X = (document.DefaultPageSettings.Bounds.Width-document.Margins.Right+document.Margins.Left)/2 - Width/2;
break;
case ICustomPaint.HorizontalAlignmentTypes.Right:
this.mRegion.X = document.DefaultPageSettings.Bounds.Right - document.Margins.Right - Width;
break;
case ICustomPaint.HorizontalAlignmentTypes.Left:
this.mRegion.X = document.Margins.Left;
break;
}
}
}
/// <summary>
/// Gets or sets the height of the TextField
/// </summary>
[Description("The height of the element.")]
public override int Height
{
get {return this.mRegion.Height;}
set
{
if (this.VerticalAlignment == ICustomPaint.VerticalAlignmentTypes.None || this.VerticalAlignment == ICustomPaint.VerticalAlignmentTypes.Top)
this.mRegion.Height = value;
else if (this.VerticalAlignment == ICustomPaint.VerticalAlignmentTypes.Bottom)
{
this.mRegion.Y = this.mRegion.Y - value + this.mRegion.Height;
this.mRegion.Height = value;
}
else if (this.VerticalAlignment == ICustomPaint.VerticalAlignmentTypes.Middle)
{
this.mRegion.Y = this.mRegion.Y - value/2 + this.mRegion.Height/2;
this.mRegion.Height = value;
}
}
}
/// <summary>
/// Gets or sets the TextField repeatability over multiple pages
/// </summary>
/// <remarks>
/// The TextField's repeatability setting governs how whether it is repeated
/// over multiple pages, only on the first page, or only on the last page.
/// </remarks>
[Description("This controls repeatability of the TextField over multiple pages.")]
public override ICustomPaint.ObjectRepeatabilityTypes ObjectRepeatability
{
get
{
return this.objectRepeatability;
}
set
{
this.objectRepeatability = value;
}
}
/// <summary>
/// Gets/Sets whether the TextField is selectable in the design pane of the DaReport Designer
/// </summary>
/// <remarks>If set to true, then the TextField is not selectable in the DaReport Designer application
/// design pane. It is still selectable in the tree view listing of objects.
/// </remarks>
[Description("Sets whether the object can be selected in the design pane.")]
public override bool Selectable
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -