views:

410

answers:

1

I'm trying to make a trackbar with a custom graphic for the slider thumb. I have started out with the following code:

namespace testapp
{
    partial class MyTrackBar : System.Windows.Forms.TrackBar
    {
        public MyTrackBar()
        {
            InitializeComponent();
        }

        protected override void  OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
        //   base.OnPaint(e);
            e.Graphics.FillRectangle(System.Drawing.Brushes.DarkSalmon, ClientRectangle);
        }
    }
}

But it never calls OnPaint. Anyone else come across this? I have used this technique before to create an ownerdraw button but for some reason it doesn't work with TrackBar.

PS. Yes, I have seen question #625728 but the solution there was to completely re-implement the control from scratch. I just want to modify the existing control a little.

A: 

I've solved it by setting the UserPaint style in the constructor like so:

public MyTrackBar()
{
    InitializeComponent();
    SetStyle(ControlStyles.UserPaint, true);
}

OnPaint now gets called.

Adam Pierce