Extended Label Control

3:46 AM
Here comes a Custom Label Control

A pic to controls Extended Properties and Control




i have created a smart tag for this one.. like those in Microsoft controls..
See the below Pic

Here is the Coding :

This Control Inherits the Label so:
public partial class ExtendedLabelR : Label


Add the Below Line before the custom control class defination (for Smart Tag to work)
[Designer(typeof(ExtendedLabelRDesigner))]


Properties:
private Boolean _Left = false;
        [Browsable(true), Category("Extended Properties")]
        public Boolean LeftLine
        {
            get { return _Left; }
            set
            {
                _Left = value;
                Invalidate();
            }
        }

        private Boolean _Right = false;
        [Browsable(true), Category("Extended Properties")]
        public Boolean RightLine
        {
            get { return _Right; }
            set
            {
                _Right = value;
                Invalidate();
            }
        }

        private Boolean _Top = false;
        [Browsable(true), Category("Extended Properties")]
        public Boolean TopLine
        {
            get { return _Top; }
            set
            {
                _Top = value;
                Invalidate();
            }
        }

        private Boolean _Bottom = false;
        [Browsable(true), Category("Extended Properties")]
        public Boolean BottomLine
        {
            get { return _Bottom; }
            set
            {
                _Bottom = value;
                Invalidate();
            }
        }

        private Boolean _All = false;
        [Browsable(true), Category("Extended Properties")]
        public Boolean All
        {
            get { return _All; }
            set
            {
                _All = value;
                _Left = false;
                _Right = false;
                _Top = false;
                _Bottom = false;
                Invalidate();
            }
        }

        private System.Drawing.Drawing2D.DashStyle _LineStype;
        [Browsable(true), Category("Extended Properties")]
        public System.Drawing.Drawing2D.DashStyle LineStype
        {
            get { return _LineStype; }
            set
            {
                _LineStype = value;
                Invalidate();
            }
        }

        private Color _LineColor;
        [Browsable(true), Category("Extended Properties")]
        public Color LineColor
        {
            get { return _LineColor; }
            set { _LineColor = value; Invalidate(); }
        }

        private float  _LineWidth=1;
        [Browsable(true), Category("Extended Properties")]
        public float  LineWidth
        {
            get { return _LineWidth; }
            set { _LineWidth = value; Invalidate(); }
        }

Now we will override the OnPaintBackground for the Lines Painting

protected override void OnPaintBackground(PaintEventArgs e)
        {
            base.OnPaintBackground(e);

            using (Pen p = new Pen(_LineColor,_LineWidth))
            {
                p.DashStyle = _LineStype;
                Point start;
                Point end;

                if (_All)
                {
                    e.Graphics.DrawRectangle(p, 1, 1, this.Width - 2, this.Height - 2);
                }
                else
                {

                    if (_Left)
                    {
                        start = new Point(1, 1);
                        end = new Point(1, this.Height - 1);
                        e.Graphics.DrawLine(p, start, end);
                    }

                    if (_Right)
                    {
                        start = new Point(this.Width - 1, 1);
                        end = new Point(this.Width - 1, this.Height - 1);
                        e.Graphics.DrawLine(p, start, end);
                    }

                    if (_Top)
                    {
                        start = new Point(1, 1);
                        end = new Point(this.Width - 1, 1);
                        e.Graphics.DrawLine(p, start, end);
                    }

                    if (_Bottom)
                    {
                        start = new Point(1, this.Height - 1);
                        end = new Point(this.Width - 1, this.Height - 1);
                        e.Graphics.DrawLine(p, start, end);
                    }
                }
                
            }
        }

Smart Tag Coding

#region "Smart Tag Code"
    [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
    public class ExtendedLabelRDesigner : System.Windows.Forms.Design.ControlDesigner
    {
        private DesignerActionListCollection actionLists;
        public override DesignerActionListCollection ActionLists
        {
            get
            {
                if (null == actionLists)
                {
                    actionLists = new DesignerActionListCollection();
                    actionLists.Add(new ExtendedLabelRActionList(this.Component));
                }
                return actionLists;
            }
        }
    }

    public class ExtendedLabelRActionList : System.ComponentModel.Design.DesignerActionList
    {
        private ExtendedLabelR objExtendedLabel;
        private DesignerActionUIService designerActionUISvc = null;

        public ExtendedLabelRActionList(IComponent component)    : base(component) 
        {
            this.objExtendedLabel = component as ExtendedLabelR;
            // Cache a reference to DesignerActionUIService, so the
            // DesigneractionList can be refreshed.
            this.designerActionUISvc =GetService(typeof(DesignerActionUIService)) as DesignerActionUIService;
        }

        private PropertyDescriptor GetPropertyByName(String propName)
        {
            PropertyDescriptor prop;
            prop = TypeDescriptor.GetProperties(objExtendedLabel)[propName];
            if (null == prop)
                throw new ArgumentException("Matching ExtendedLabelR property not found!",propName);
            else
                return prop;
        }

        
        public Boolean LeftLine
        {
            get { return objExtendedLabel.LeftLine; }
            set
            {
                GetPropertyByName("LeftLine").SetValue(objExtendedLabel, value);
            }
        }

        public Boolean RightLine
        {
            get { return objExtendedLabel.RightLine ; }
            set
            {
                GetPropertyByName("RightLine").SetValue(objExtendedLabel, value);
            }
        }

        public Boolean TopLine
        {
            get { return objExtendedLabel.TopLine ; }
            set
            {
                GetPropertyByName("TopLine").SetValue(objExtendedLabel, value);
            }
        }

        public Boolean BottomLine
        {
            get { return objExtendedLabel.BottomLine ; }
            set
            {
                GetPropertyByName("BottomLine").SetValue(objExtendedLabel, value);
            }
        }

        public Boolean All
        {
            get { return objExtendedLabel.All; }
            set
            {
                GetPropertyByName("All").SetValue(objExtendedLabel, value);
            }
        }

        public float LineWidth
        {
            get { return objExtendedLabel.LineWidth; }
            set
            {
                GetPropertyByName("LineWidth").SetValue(objExtendedLabel, value);
            }
        }
        public Color LineColor
        {
            get { return objExtendedLabel.LineColor; }
            set 
            {
                GetPropertyByName("LineColor").SetValue(objExtendedLabel, value);
            }
        }

        public override DesignerActionItemCollection GetSortedActionItems()
        {
            DesignerActionItemCollection items = new DesignerActionItemCollection();
            items.Add(new DesignerActionHeaderItem("Draw Lines"));

            items.Add(new DesignerActionPropertyItem("LeftLine","Left Line","Draw Lines","Should Draw Left Line?"));
            items.Add(new DesignerActionPropertyItem("RightLine", "Right Line", "Draw Lines", "Should Draw Right Line?"));
            items.Add(new DesignerActionPropertyItem("TopLine", "Top Line", "Draw Lines", "Should Draw Top Line?"));
            items.Add(new DesignerActionPropertyItem("BottomLine", "Bottom Line", "Draw Lines", "Should Draw Bottom Line?"));
            items.Add(new DesignerActionPropertyItem("All", "All Line", "Draw Lines", "Should Draw All Line?"));
            items.Add(new DesignerActionPropertyItem("LineWidth", "Line Width", "Draw Lines", "Width of The Line"));
            items.Add(new DesignerActionPropertyItem("LineColor", "Line Color", "Draw Lines", "Color of The Line"));
            return items;
        }

    }
    #endregion

If you want the Complete Source Code then mail me on : sandeepparekh9@gmaill.com

1 comments:

Post a Comment