Click here to Skip to main content
15,887,676 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.5K   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.Drawing ;
using System.ComponentModel;

namespace FormPrintComponent
{
    /// <summary>
    /// The style settings to use to draw the borders of the 
    /// bounding rectangle of a print setting area
    /// </summary>
    public class BorderStyle
    {

        #region Private members
        private Color _leftColour = Color.Transparent;
        private Color _rightColour = Color.Transparent;
        private Color _topColour = Color.Transparent;
        private Color _bottomColour = Color.Transparent;

        private float _leftWidth = 1;
        private float _rightWidth = 1;
        private float _topWidth = 1;
        private float _bottomWidth = 1;

        #endregion
        
        /// <summary>
        /// The colour to use to draw the left hand border of this 
        /// bounding rectangle
        /// </summary>
        public Color LeftColour
        {
            get { return _leftColour; }
            set { _leftColour = value; }
        }

        /// <summary>
        /// The colour to use to draw the right hand of the bounding rectangle
        /// </summary>
        public Color RightColour
        {
            get { return _rightColour; }
            set { _rightColour = value; }
        }

        /// <summary>
        /// The colour to use to draw the top border of the bounding rectangle
        /// </summary>
        public Color TopColour
        {
            get { return _topColour; }
            set { _topColour = value; }
        }

        /// <summary>
        /// The colour to use to draw the bottom border of the bounding rectangle
        /// </summary>
        public Color BottomColour
        {
            get { return _bottomColour; }
            set { _bottomColour = value; }
        }

        /// <summary>
        /// The width to draw the left hand border of the bounding rectangle
        /// </summary>
        public float LeftWidth
        {
            get { return _leftWidth; }
            set { _leftWidth = value; }
        }

        /// <summary>
        /// The width to draw the right hand border of the bounding rectangle
        /// </summary>
        public float RightWidth
        {
            get { return _rightWidth; }
            set { _rightWidth = value; }
        }

        /// <summary>
        /// The width to use to draw the top border of the bounding rectangle
        /// </summary>
        public float TopWidth
        {
            get { return _topWidth; }
            set { _topWidth = value; }
        }

        /// <summary>
        /// The width to draw the bottom border of the bounding rectangle with
        /// </summary>
        public float BottomWidth
        {
            get { return _bottomWidth; }
            set { _bottomWidth = value; }
        }

        #region Friend methods

        /// <summary>
        /// Get the pen used to draw the left hand border
        /// </summary>
        internal Pen GetLeftPen()
        {
            return new Pen(_leftColour, LeftWidth);
        }

        /// <summary>
        /// Gets the pen used to draw the right hand border
        /// </summary>
        internal Pen GetRightPen()
        {
            return new Pen(_rightColour, _rightWidth);
        }

        /// <summary>
        /// Gets the pen used to draw the top border
        /// </summary>
        internal Pen GetTopPen()
        {
            return new Pen(_topColour, _topWidth);
        }

        /// <summary>
        /// Get the pen used to draw the bottom border of the bounding rectangle
        /// </summary>
        internal Pen GetBottomPen()
        {
            return new Pen(_bottomColour, _bottomWidth);
        }

        #endregion

        #region Public constructors
        public BorderStyle()
        {
        }

        /// <summary>
        /// Creates a new border with all four sides the given colour
        /// </summary>
        /// <param name="borderColour">
        /// The colour to use for the border
        /// </param>
        /// <remarks >
        /// By default the border width is 1.
        /// </remarks>
        public BorderStyle(Color borderColour)
        {
            _rightColour = borderColour;
            _leftColour = borderColour;
            _bottomColour = borderColour;
            _topColour = borderColour;
        }

        /// <summary>
        /// Creates a new border with all four sides the given colour
        /// </summary>
        /// <param name="borderColour">
        /// The colour to use for the border
        /// </param>
        /// <param name="borderWidth" >
        /// 
        /// </param>
        public BorderStyle(Color borderColor, float borderWidth)
            : this(borderColor)
        {
            _rightWidth = borderWidth;
            _leftWidth = borderWidth;
            _topWidth = borderWidth;
            _bottomWidth = borderWidth;
        }

        #endregion

    }

    namespace Design
    {
        /// <summary>
        /// The type converte that converts a border style to/from a string
        /// </summary>
        public class BorderStyleConverter :
            TypeConverter
        {

            /// <summary>
            /// True if you can convert the input thing to a border style
            /// </summary>
            /// <param name="context"></param>
            /// <param name="sourceType"></param>
            /// <returns></returns>
            public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
            {
                if (sourceType == typeof(string))
                {
                    return true;
                }
                else
                {
                    return base.CanConvertFrom(context, sourceType);
                }
            }


            public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
            {
                if (destinationType == typeof(string))
                {
                    return true;
                }
                else
                {
                    return base.CanConvertTo(context, destinationType);
                }
            }

            public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
            {
                if (destinationType == typeof(string))
                {
                    if (value.GetType() == typeof(BorderStyle ))
                    {
                        System.Drawing.ColorConverter _myConverter = new System.Drawing.ColorConverter();

                        return string.Concat((_myConverter.ConvertToInvariantString(((BorderStyle)value).LeftColour)), @",", ((((BorderStyle)value)).LeftWidth.ToString()) , @"," ,
                            _myConverter.ConvertToInvariantString(((BorderStyle)value).RightColour), @",", ((((BorderStyle)value)).RightWidth.ToString()), @"," ,
                                _myConverter.ConvertToInvariantString(((BorderStyle)value).TopColour), @",", ((((BorderStyle)value)).TopWidth.ToString()), @"," ,
                                    _myConverter.ConvertToInvariantString(((BorderStyle)value).BottomColour), @",", ((((BorderStyle)value)).BottomWidth.ToString()
                            ));
                    }
                    else
                    {
                        return base.ConvertTo(context, culture, value, destinationType);
                    }
                }
                else
                {
                    return base.ConvertTo(context, culture, value, destinationType);
                }
            }

            public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
            {
                if (value.GetType() == typeof(string))
                {

                    BorderStyle _bs = new BorderStyle();
                    
                    int _penWidth = 1;
                    string[] _sections = ((string)value).Split(',');
                    if (_sections.Length > 0)
                    {
                        System.Drawing.ColorConverter _myConverter = new System.Drawing.ColorConverter();
                        _bs.LeftColour = (System.Drawing.Color)_myConverter.ConvertFromInvariantString(context, _sections[0]);
                    }
                    if (_sections.Length > 1)
                    {
                        if (!(int.TryParse(_sections[1], out _penWidth)))
                        {
                            _penWidth = 1;
                        }
                        _bs.LeftWidth = _penWidth;
                    }
                    if (_sections.Length > 2)
                    {
                        System.Drawing.ColorConverter _myConverter = new System.Drawing.ColorConverter();
                        _bs.RightColour = (System.Drawing.Color)_myConverter.ConvertFromInvariantString(context, _sections[2]);
                    }
                    if (_sections.Length > 3)
                    {
                        if (!(int.TryParse(_sections[3], out _penWidth)))
                        {
                            _penWidth = 1;
                        }
                        _bs.RightWidth = _penWidth;
                    }
                    if (_sections.Length > 4)
                    {
                        System.Drawing.ColorConverter _myConverter = new System.Drawing.ColorConverter();
                        _bs.TopColour = (System.Drawing.Color)_myConverter.ConvertFromInvariantString(context, _sections[4]);
                    }
                    if (_sections.Length > 5)
                    {
                        if (!(int.TryParse(_sections[5], out _penWidth)))
                        {
                            _penWidth = 1;
                        }
                        _bs.TopWidth = _penWidth;
                    }
                    if (_sections.Length > 6)
                    {
                        System.Drawing.ColorConverter _myConverter = new System.Drawing.ColorConverter();
                        _bs.BottomColour = (System.Drawing.Color)_myConverter.ConvertFromInvariantString(context, _sections[6]);
                    }
                    if (_sections.Length > 7)
                    {
                        if (!(int.TryParse(_sections[7], out _penWidth)))
                        {
                            _penWidth = 1;
                        }
                        _bs.BottomWidth = _penWidth;
                    }

                    return _bs ;
                }
                else
                {
                    return base.ConvertFrom(context, culture, value);
                }
            }


        }
    }
}

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