Showing posts with label Custom Controls. Show all posts
Showing posts with label Custom Controls. Show all posts

Gradient Panel With Shadow Support and Lots of Other Stuff

11:25 PM
Properties:

Source Code:


Variables
PointF pf;
        SizeF sf = new SizeF();

        public  enum ImageAlign
        {
            Custom,
            TopLeft,
            TopCenter,
            TopRight,
            MiddleLeft,
            MiddleCenter,
            MiddleRight,
            BottomLeft,
            BottomCenter,
            BottomRight
        }

        public enum TextAlign
        {
            Custom,
            TopLeft,
            TopCenter,
            TopRight,
            MiddleLeft,
            MiddleCenter,
            MiddleRight,
            BottomLeft,
            BottomCenter,
            BottomRight
        }


Properties
TextAlign  _TextAlign = TextAlign.TopLeft;
        [Browsable(true), Category("GradientPanel")]
        public TextAlign TxtAlign
        {
            get { return _TextAlign; }
            set { _TextAlign = value; Invalidate(); }
        }

        String  _Text;
        [Browsable(true), Category("GradientPanel")]
        public override string Text
        {
            get { return _Text; }
            set { _Text = value; Invalidate(); }
        }

        private float _TextMargin = 1.0f;
        [Browsable(true), Category("GradientPanel")]
        [DefaultValue("1.0f")]
        public float TextMargin
        {
            get { return _TextMargin; }
            set { _TextMargin = value; Invalidate(); }
        }
        
        ImageAlign _ImageAlign= ImageAlign.TopLeft;
        [Browsable(true), Category("GradientPanel")]
        public ImageAlign ImgAlign
        {
            get { return _ImageAlign; }
            set { _ImageAlign = value; Invalidate(); }
        }

        Image _Image;
        [Browsable(true), Category("GradientPanel")]
        public Image Image
        {
            get { return _Image; }
            set { _Image = value; Invalidate(); }
        }

        Point _ImageLocation = new Point(4, 4);
        [Browsable(true), Category("GradientPanel")]
        [DefaultValue("4,4")]
        public Point ImageLocation
        {
            get { return _ImageLocation; }
            set { _ImageLocation = value; Invalidate(); }
        }

        private float _ImageMargin = 1.0f;
        [Browsable(true), Category("GradientPanel")]
        [DefaultValue("1.0f")]
        public float ImageMargin
        {
            get { return _ImageMargin; }
            set { _ImageMargin = value; Invalidate(); }
        }


        int _BorderWidth = 1;
        [Browsable(true), Category("GradientPanel")]
        [DefaultValue(1)]
        public int BorderWidth
        {
            get { return _BorderWidth; }
            set { _BorderWidth = value; Invalidate(); }
        }

        int _ShadowOffSet = 5;
        [Browsable(true), Category("GradientPanel")]
        [DefaultValue(5)]
        public int Shadow
        {
            get
            {
                return _ShadowOffSet;
            }
            set { _ShadowOffSet = Math.Abs(value); Invalidate(); }
        }

         int _RoundCornerRadius = 4;
        [Browsable(true), Category("GradientPanel")]
        [DefaultValue(4)]
        public int CornerRadius
        {
            get { return _RoundCornerRadius; }
            set { _RoundCornerRadius = Math.Abs(value); Invalidate(); }
        }
      
        Color _BorderColor = Color.Gray;
        [Browsable(true), Category("GradientPanel")]
        [DefaultValue("Color.Gray")]
        public Color BorderColor
        {
            get { return _BorderColor; }
            set { _BorderColor = value; Invalidate(); }
        }

        Color _GradientStartColor = Color.White;
        [Browsable(true), Category("GradientPanel")]
        [DefaultValue("Color.White")]
        public Color GradientStartColor
        {
            get { return _GradientStartColor; }
            set { _GradientStartColor = value; Invalidate(); }
        }

        Color _GradientEndColor = Color.Gray;
        [Browsable(true), Category("GradientPanel")]
        [DefaultValue("Color.Gray")]
        public Color GradientEndColor
        {
            get { return _GradientEndColor; }
            set { _GradientEndColor = value; Invalidate(); }
        }

        LinearGradientMode _LinearGradientMode = LinearGradientMode.Vertical;
        [Browsable(true), Category("GradientPanel")]
        public LinearGradientMode GradientMode
        {
            get { return _LinearGradientMode; }
            set { _LinearGradientMode = value; Invalidate(); }
        }

        Boolean _DrawLine = false;
        [Browsable(true), Category("GradientPanel")]
        public Boolean DrawLine
        {
            get { return _DrawLine; }
            set { _DrawLine = value; Invalidate(); }
        }

        Color _LineColor = Color.Black;
        [Browsable(true), Category("GradientPanel")]
        public Color LineColor
        {
            get { return _LineColor; }
            set { _LineColor = value; Invalidate(); }
        }


Constructor
public GradientPanelR()
        {
            this.SetStyle(ControlStyles.DoubleBuffer, true);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.ResizeRedraw, true);
            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            InitializeComponent();
        }

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

            int tmpShadowOffSet = Math.Min(Math.Min(_ShadowOffSet, this.Width - 2), this.Height - 2);
            int tmpSoundCornerRadius = Math.Min(Math.Min(_RoundCornerRadius, this.Width - 2), this.Height - 2);
            if (this.Width > 1 && this.Height > 1)
            {
                e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

                Rectangle rect = new Rectangle(0, 0, this.Width - tmpShadowOffSet - 1, this.Height - tmpShadowOffSet - 1);
                Rectangle rectShadow = new Rectangle(tmpShadowOffSet, tmpShadowOffSet, this.Width - tmpShadowOffSet - 1, this.Height - tmpShadowOffSet - 1);

                GraphicsPath graphPathShadow = GetRoundPath(rectShadow, tmpSoundCornerRadius);
                GraphicsPath graphPath = GetRoundPath(rect, tmpSoundCornerRadius);

                if (tmpSoundCornerRadius > 0)
                {
                    using (PathGradientBrush gBrush = new PathGradientBrush(graphPathShadow))
                    {
                        gBrush.WrapMode = WrapMode.Clamp;
                        ColorBlend colorBlend = new ColorBlend(3);
                        colorBlend.Colors = new Color[]{Color.Transparent, 
             Color.FromArgb(180, Color.DimGray), 
             Color.FromArgb(180, Color.DimGray)};

                        colorBlend.Positions = new float[] { 0f, .1f, 1f };

                        gBrush.InterpolationColors = colorBlend;
                        e.Graphics.FillPath(gBrush, graphPathShadow);
                    }
                }

                // Draw backgroud
                LinearGradientBrush brush = new LinearGradientBrush(rect,
                    this._GradientStartColor,
                    this._GradientEndColor,
                   GradientMode);
                e.Graphics.FillPath(brush, graphPath);
                e.Graphics.DrawPath(new Pen(Color.FromArgb(180, this._BorderColor), _BorderWidth), graphPath);

                // Draw Image
                if (_Image != null)
                {
                    //e.Graphics.DrawImageUnscaled(_Image, _ImageLocation);
                    DrawImage(e.Graphics);
                }


            }
            DrawText(e.Graphics);
        }

Drawing Image
#region DrawImage
        public void DrawImage(Graphics g)
        {
            

            if (this.Image != null && _ImageAlign != ImageAlign.Custom)
            {
                pf = new PointF(0, 0);

                if (_ImageAlign  == ImageAlign.TopLeft)
                {
                    pf.X = 1; pf.Y = 1;
                    pf.X += _ImageMargin;
                    pf.Y += _ImageMargin;

                    if (_DrawLine)
                    {
                       // HatchBrush hbr = new HatchBrush(HatchStyle.ZigZag , _LineColor );
                        Pen P = new Pen(_LineColor, 3);
                        g.DrawLine(P, new PointF(Image.Width + pf.X + 2 + 10, Image.Height + pf.Y + 2), new PointF(Width - 10, Image.Height + pf.Y + 2));
                    }

                }
                if (_ImageAlign == ImageAlign.TopCenter)
                {

                    pf.X = ((float)Width - (float)Image.Width) / 2;
                    pf.Y += _ImageMargin;
                }
                if (_ImageAlign == ImageAlign.TopRight)
                {

                    pf.X = (float)Width - (float)Image.Width;
                    pf.Y = 1;
                    pf.X -= _ImageMargin;
                    pf.Y += _ImageMargin;
                }

                if (_ImageAlign == ImageAlign.MiddleLeft)
                {

                    pf.X = 1;
                    pf.Y = ((float)Height - (float)Image.Height) / 2;
                    pf.X += _ImageMargin;

                }

                if (_ImageAlign == ImageAlign.MiddleCenter)
                {
                    pf.X = ((float)Width - (float)Image.Width) / 2;
                    pf.Y = ((float)Height - (float)Image.Height) / 2;

                }
                if (_ImageAlign == ImageAlign.MiddleRight)
                {
                    pf.X = (float)Width - (float)Image.Width;
                    pf.Y = ((float)Height - (float)Image.Height) / 2;
                    pf.X -= _ImageMargin;

                }

                if (_ImageAlign == ImageAlign.BottomLeft)
                {
                    pf.X = 1;
                    pf.Y = ((float)Height - (float)Image.Height);
                    pf.X += _ImageMargin;
                    pf.Y -= _ImageMargin;
                }
                if (_ImageAlign == ImageAlign.BottomCenter)
                {
                    pf.X = ((float)Width - (float)Image.Width) / 2;
                    pf.Y = ((float)Height - (float)Image.Height);

                    pf.Y -= _ImageMargin;
                }
                if (_ImageAlign == ImageAlign.BottomRight)
                {
                    pf.X = (float)Width - (float)Image.Width;
                    pf.Y = ((float)Height - (float)Image.Height);

                    pf.X -= _ImageMargin;
                    pf.Y -= _ImageMargin;

                }

                g.DrawImage(this.Image, pf);
            }
            if (this.Image != null && _ImageAlign == ImageAlign.Custom)
            {
                pf = new PointF(0, 0);
                pf.X = _ImageLocation.X ;
                pf.Y = _ImageLocation.Y;
                
                g.DrawImage(this.Image, pf);
            }
        }
        #endregion

Drawing Text
#region DrawText
          public void DrawText(Graphics g)
        {
            SolidBrush b = new SolidBrush(this.ForeColor);

            pf = new PointF();
            sf = g.MeasureString(this.Text, this.Font);

            //calculate textalign
            if (_TextAlign == TextAlign.TopLeft)
            {
                pf.X = 1 + _TextMargin ; pf.Y = 1 + _TextMargin ;
            }
            if (this._TextAlign == TextAlign.TopCenter)
            {
                
                pf.X = ((float)Width - sf.Width) / 2;
                pf.Y =1 + _TextMargin ; 
            }
            if (this._TextAlign == TextAlign.TopRight)
            {
                
                pf.X = (float)Width - sf.Width  - _TextMargin ;
                pf.Y = 1 + _TextMargin ;
            }

            if (this._TextAlign == TextAlign.MiddleLeft)
            {
                
                pf.X = 1 + _TextMargin ;
                pf.Y =( (float)Height - sf.Height) / 2;
            }

            if (this._TextAlign == TextAlign.MiddleCenter)
            {
                pf.X = ((float)Width - sf.Width) / 2;
                pf.Y = ((float)Height - sf.Height) / 2;
            }
            if (this._TextAlign == TextAlign.MiddleRight)
            {
                pf.X = (float)Width - sf.Width - _TextMargin ;
                pf.Y = ((float)Height - sf.Height) / 2;
            }

            if (this._TextAlign == TextAlign.BottomLeft)
            {
                pf.X = 1 + _TextMargin ;
                pf.Y = ((float)Height - sf.Height) - _TextMargin ;
            }
            if (this._TextAlign == TextAlign.BottomCenter)
            {
                pf.X = ((float)Width - sf.Width) / 2;
                pf.Y = ((float)Height - sf.Height) - _TextMargin ;
            }
            if (this._TextAlign == TextAlign.BottomRight)
            {
                pf.X = (float)Width - sf.Width - _TextMargin;
                pf.Y = ((float)Height - sf.Height) - _TextMargin ;
            }

            //g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias ;
            g.DrawString(this.Text, this.Font, b, pf);
            
        }
       
        #endregion

Other Helping Methods
public static GraphicsPath GetRoundPath(Rectangle r, int depth)
        {
            GraphicsPath graphPath = new GraphicsPath();

            graphPath.AddArc(r.X, r.Y, depth, depth, 180, 90);
            graphPath.AddArc(r.X + r.Width - depth, r.Y, depth, depth, 270, 90);
            graphPath.AddArc(r.X + r.Width - depth, r.Y + r.Height - depth, depth, depth, 0, 90);
            graphPath.AddArc(r.X, r.Y + r.Height - depth, depth, depth, 90, 90);
            graphPath.AddLine(r.X, r.Y + r.Height - depth, r.X, r.Y + depth / 2);

            return graphPath;
        }

For Complete source mail me at : sandeepparekh9@gmail.com

A Transparent Textbox

5:13 AM

I have tried so hard to find a transparent textbox on the web but all in vain.
so i have tried to create a one..

This a User Control not a Custom Control so it has less functionality that the traditional texbox but i have tried to cover most of the important properties.

here are some pics..





This Control is inherited from UserControl


Properties:


#region Properties
        [Browsable(false), Category("Appearance")]
        public override Font Font
        {
            get
            {
                return base.Font;
            }
            set
            {
                base.Font = value;
            }
        }

        [Browsable(false), Category("Appearance")]
        public override Color ForeColor
        {
            get
            {
                return base.ForeColor;
            }
            set
            {
                base.ForeColor = value;
            }
        }

        [Browsable(false), Category("Appearance")]
        public override Color BackColor
        {
            get
            {
                return base.BackColor;
            }
            set
            {
                base.BackColor = value;
            }
        }

        [Browsable(false), Category("Appearance")]
        public override Image BackgroundImage
        {
            get
            {
                return base.BackgroundImage;
            }
            set
            {
                base.BackgroundImage = value;
            }
        }

        [Browsable(false), Category("Appearance")]
        public override ImageLayout BackgroundImageLayout
        {
            get
            {
                return base.BackgroundImageLayout;
            }
            set
            {
                base.BackgroundImageLayout = value;
            }
        }


        [Browsable(true), Category("Appearance")]
        public  Font  _Font
        {
            get { return this.Font; }
            set
            {
                txt.Font = value;
                lbl.Font = value;
                Invalidate();
            }
        }

        [Browsable(true), Category("Appearance")]
        public  Color _ForeColor
        {
            get { return this.ForeColor; }
            set
            {
                txt.ForeColor = value;
                lbl.ForeColor = value;
                this.ForeColor = value;
                Invalidate();
            }
        }

        [Browsable(true), Category("Appearance")]
        public  Color _BackColor
        {
            get { return this.BackColor; }
            set
            {
                if (value != Color.Transparent) { txt.BackColor = value; this.BackColor = value; }
                Invalidate();
            }
        }

        [Browsable(true), Category("Appearance")]
        public ScrollBars _ScrollBars
        {
            get { return txt.ScrollBars; }
            set { txt.ScrollBars = value; Invalidate(); }
        }

        [Browsable(true), Category("Appearance")]
        public string Text
        {
            get { return txt.Text; }
            set { txt.Text = value; lbl.Text = value; Invalidate(); }
        }

        [Browsable(true), Category("Appearance")]
        public string[] _Lines
        {
            get { return txt.Lines; }
            set { txt.Lines = value; Invalidate(); }

        }

        [Browsable(true), Category("Appearance")]
        public HorizontalAlignment _TextAlignMent
        {
            get { return txt.TextAlign; }
            set 
            {   txt.TextAlign = value;
                if (value == HorizontalAlignment.Center) { lbl.TextAlign = ContentAlignment.TopCenter; }
                if (value == HorizontalAlignment.Left ) { lbl.TextAlign = ContentAlignment.TopLeft ; }
                if (value == HorizontalAlignment.Right) { lbl.TextAlign = ContentAlignment.TopRight ; }
                Invalidate();
            }
        }

        [Browsable(true), Category("Behavior")]
        public bool AcceptsTab
        {
            get { return txt.AcceptsTab; }
            set { txt.AcceptsTab = value; Invalidate(); }
        }

        [Browsable(true), Category("Behavior")]
        public bool AcceptsReturn
        {
            get { return txt.AcceptsReturn ; }
            set { txt.AcceptsReturn = value; Invalidate(); }
        }

        [Browsable(true), Category("Behavior")]
        public override bool AllowDrop
        {
            get { return txt.AllowDrop; }
            set { txt.AllowDrop = value; lbl.AllowDrop = value; Invalidate(); }
            
        }

        [Browsable(true), Category("Behavior")]
        public CharacterCasing CharacterCasing
        {
            get { return txt.CharacterCasing; }
            set { txt.CharacterCasing = value; Invalidate(); }
        }

        [Browsable(true), Category("Behavior")]
        public bool HideSelection
        {
            get { return txt.HideSelection; }
            set { txt.HideSelection = value; Invalidate(); }
        }

        [Browsable(true), Category("Behavior")]
        public int MaxLength
        {
            get { return txt.MaxLength; }
            set { txt.MaxLength = value; Invalidate(); }
        }

        [Browsable(true), Category("Behavior")]
        public char PasswordChar
        {
            get { return txt.PasswordChar; }
            set { 
                    txt.PasswordChar = value;
                    lbl.Text ="";
                    for (int i = 0; i < value.ToString().Length; i++)
                    {
                        lbl.Text += PasswordChar;
                    }
                    Invalidate();
                }
        }

        [Browsable(true), Category("Behavior")]
        public bool Multiline
        {
            get { return txt.Multiline; }
            set { txt.Multiline = value; }
        }

        [Browsable(true), Category("Behavior")]
        public bool ReadOnly
        {
            get { return txt.ReadOnly; }
            set { txt.ReadOnly = value; }
        }

        [Browsable(true), Category("Behavior")]
        public bool ShortcutsEnabled
        {
            get { return txt.ShortcutsEnabled; }
            set { txt.ShortcutsEnabled = value; Invalidate(); }
        }

        [Browsable(true), Category("Behavior")]
        public bool UseSystemPasswordChar
        {
            get { return txt.UseSystemPasswordChar; }
            set { txt.UseSystemPasswordChar = value; Invalidate(); }
        }
        
        [Browsable(true), Category("Behavior")]
        public bool WordWrap
        {
            get { return txt.WordWrap; }
            set { txt.WordWrap = value; }
        }

        [Browsable(true), Category("Misc")]
        public AutoCompleteStringCollection AutoCompleteCustomSource
        {
            get { return txt.AutoCompleteCustomSource; }
            set { txt.AutoCompleteCustomSource = value; }
        }

        [Browsable(true), Category("Misc")]
        public AutoCompleteMode AutoCompleteMode
        {
            get { return txt.AutoCompleteMode;  }
            set { txt.AutoCompleteMode= value; }
        }

        [Browsable(true), Category("Misc")]
        public AutoCompleteSource  AutoCompleteSource
        {
            get { return txt.AutoCompleteSource;  }
            set { txt.AutoCompleteSource = value; }
        }
        #endregion

        #region Events
        [Browsable(true), Category("Misc")]
        public event EventHandler TextChanged
        {
            add { txt.TextChanged += value; }
            remove { txt.TextChanged -= value; }
        }

        #endregion
Other Importand Event and Methods
#region Events
        [Browsable(true), Category("Misc")]
        public event EventHandler TextChanged
        {
            add { txt.TextChanged += value; }
            remove { txt.TextChanged -= value; }
        }

        #endregion

        private void lbl_MouseEnter(object sender, EventArgs e)
        {
            if (txt.PasswordChar != '\0')// && txt.PasswordChar.ToString() != "")
            {
                lbl.Text = "";
                for (int i = 0; i < txt.Text.ToString().Length; i++)
                {
                    lbl.Text += PasswordChar;
                }
            }
            else
            {
                lbl.Text = txt.Text;
            }
            txt.BringToFront();
            lbl.SendToBack();
            txt.Focus();
        }
        public void Clear()
        {
            txt.Clear();
            lbl.Text = "";
        }

        private void txt_MouseLeave(object sender, EventArgs e)
        {
            
            txt.SendToBack();
            lbl.BringToFront();
            if (txt.PasswordChar != '\0')
            {
                lbl.Text = "";
                for (int i = 0; i < txt.Text.ToString().Length; i++)
                {
                    lbl.Text += PasswordChar;
                }
            }
            else
            {
                lbl.Text = txt.Text;
            }
        }

        private void TransTextBoxR_Load(object sender, EventArgs e)
        {
            if (txt.PasswordChar != '\0')
            {
                lbl.Text = "";
                for (int i = 0; i < txt.Text.ToString().Length; i++)
                {
                    lbl.Text += PasswordChar;
                }
            }
            else
            {
                lbl.Text = txt.Text;
            }
        }
For Full Source Code email me at : sandeepparekh9@gmail.com

A Professional Looking Label

4:23 AM
A Professional Looking Label

Some Picks:
Without Glossy Effect


With Glossy Effect


Coding
This on Inherits Label So
public partial class ProfLabel : Label

Properties
  public string _Text;

        private Color _SecondBackColor = Color.Silver;
        public Color SecondBackColor
        {
            get { return _SecondBackColor; }
            set { _SecondBackColor = value; Invalidate(); }
        }

        private bool _IsGlossyeffect = false;
        public bool GlossyEffect
        {
            get { return _IsGlossyeffect; }
            set { _IsGlossyeffect = value; Invalidate(); }
        }

        private int _BottomLineWidth = 3;
        public int BottomLineWidth
        {
            get { return _BottomLineWidth; }
            set { _BottomLineWidth = value; Invalidate(); }
        }

Now Override the OnPaint Method

 protected override void OnPaint(PaintEventArgs pe)
        {
           
            pe.Graphics.FillRectangle(new SolidBrush(Color.Transparent), this.ClientRectangle);

            _Text = this.Text;
            
            this.AutoSize = false;
            SizeF size = pe.Graphics.MeasureString(_Text, this.Font);

            Rectangle r = new Rectangle();
            r.Size = new Size(int.Parse((Math.Round(size.Width)+2).ToString()),int.Parse(( Math.Round(size.Height)+2).ToString()));
            r.Location = new Point(5, 5);


            SolidBrush b = new SolidBrush(_SecondBackColor);
            pe.Graphics.FillPath(b, GetRoundPath(r, 3));


            if (_IsGlossyeffect)
            {
                Rectangle upperrect = new Rectangle();
                upperrect.Location = new Point(5, 5);
                upperrect.Width = r.Width;
                upperrect.Height = r.Height / 2;

                GraphicsPath upPath = GetRoundPath(upperrect, 3);

                using (Brush brushUpper = new LinearGradientBrush(upperrect, Color.White, _SecondBackColor, LinearGradientMode.Vertical))
                {
                    pe.Graphics.FillPath(brushUpper, upPath);
                }
            }
            
            Point pDrawStrPoint = new Point();
            pDrawStrPoint.X = 5;
            pDrawStrPoint.Y = 5;
            pe.Graphics.DrawString(_Text, this.Font, new SolidBrush(this.ForeColor), pDrawStrPoint);
            
            //Drawing Fine Line
            Point startPoint = new Point();
            startPoint.X = r.Left;
            startPoint.Y = r.Bottom;

            Point endpoint = new Point();
            endpoint.X = this.Width;
            endpoint.Y = r.Bottom;
            pe.Graphics.DrawLine(new Pen(_SecondBackColor, _BottomLineWidth), startPoint, endpoint);
         
        }
Other Helping Functions
 public static GraphicsPath GetRoundPath(Rectangle r, int depth)
        {
            GraphicsPath graphPath = new GraphicsPath();

            graphPath.AddArc(r.X, r.Y, depth, depth, 180, 90);
            graphPath.AddArc(r.X + r.Width - depth, r.Y, depth, depth, 270, 90);
            graphPath.AddArc(r.X + r.Width - depth, r.Y + r.Height - depth, depth, depth, 0, 90);
            graphPath.AddArc(r.X, r.Y + r.Height - depth, depth, depth, 90, 90);
            graphPath.AddLine(r.X, r.Y + r.Height - depth, r.X, r.Y + depth / 2);

            return graphPath;
        }

For Complete Source code email me at : sandeepparekh9@gmail.com

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