Quantcast
Viewing all articles
Browse latest Browse all 12583

Extending the ListBox Control to allow BorderColor

I'm trying to extend the ListBox Control and allow the property BorderColor. It appears the OnPaint event isn't triggering. I roughly know how to draw a single border but of course it isn't appearing. I would like to apply the BorderColor and BorderStyle to make a border, I tried this but it has an error where DrawBorder accepts an argument of type ButtonBorderStyle but a ListBox has a property type of BorderStyle. So I'm very confused of how I can go about doing this.

Here is my current code:

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace ListBoxPlus
{
    public class ListBoxPlus : ListBox
    {
        private Color borderColor = SystemColors.ControlDark;

        public Color BorderColor
        {
            get
            {
                return this.borderColor;
            }
            set
            {
                this.borderColor = value;
                this.Invalidate();
            }
        }

        public ListBoxPlus() { }

        protected override void OnPaint(PaintEventArgs e)
        {
            // breakpoint on the below line doesn't happen
            base.OnPaint(e);

            //ControlPaint.DrawBorder(e.Graphics, this.ClientRectangle, this.borderColor, this.BorderStyle);
            Pen pen = new Pen(this.borderColor);

            e.Graphics.DrawRectangle(pen, 0, 0, this.Width - 1, this.Height - 1);
        }
    }
}

Viewing all articles
Browse latest Browse all 12583

Trending Articles