Friday, June 28, 2019

WinForms: How To Add a Border To a Panel Control

To add a border to a Panel control, extend the control with the following class:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
 
class MyPanel : Panel
{
    private Color colorBorder = Color.Transparent;
 
    public MyPanel() : base()
    {
        this.SetStyle(ControlStyles.UserPaint, true);
    }
 
    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        e.Graphics.DrawRectangle(
            new Pen(
                new SolidBrush(colorBorder), 2), e.ClipRectangle);
    }
 
    public Color BorderColor
    {
        get { return colorBorder; }
        set { colorBorder = value; }
    }
}
You can then use the new BorderColor property to set the border to any color you like.

No comments:

Post a Comment

Performance: Linking a List of Child Objects To a List of Parent Objects

Many a time I've had to build a relationship where a parent object has a list of child objects linked to it, where the parent itself als...