Click here to Skip to main content
15,445,757 members
Articles / Desktop Programming / Windows Forms
Posted 13 Jul 2006


179 bookmarked

How to skin scrollbars for Panels, in C#

Rate me:
Please Sign up or sign in to vote.
4.90/5 (66 votes)
13 Jul 2006CPOL8 min read
This article shows you how to create a skinnable scrollbar as a user control, and use it in a Panel to replace the ugly Windows scrollbar.

Sample Image - customscrollbar1.gif

 Sample Image - customscrollbar2.gif


Have you ever wanted to get rid of that ugly Windows scrollbar? I know I have many many times. Until now, it has been an extremely tedious and difficult task. Back in the days before .NET, it was a huge undertaking to get rid of the default scrollbars for listviews, list control, and any other control that has the default Windows scrollbar. If you want to see just how difficult it is, check out my other article I wrote for Visual C++ 6.0 MFC, How to Skin CListCtrl including scrollbars and column headers.

Fortunately for us, we now have user controls and panels in the .NET framework. This simplifies things a lot, but it's still not easy or straightforward to customize the look and feel of Windows scrollbars and have them work properly.

Getting Started

First thing we will do is create a new C# Windows Application project called TestApp. Open Form1.cs in Design mode, and add a Panel control that is 179 pixels in height, and name it outerPanel. Then, create another Panel, but this time create it inside the OuterPanel, and name it InnerPanel. Now, set the innerPanel's AutoScroll property to True.

Next, we want to throw some kind of a control into the innerPanel so that we will have the ability to scroll in order to test our custom scrollbar. For some reason, we can not use the design editor to add a control to the innerPanel, because it messes up the DisplayRectangle property and does not return the correct value. So what we have to do is add the control to the innerPanel using code, and then everything will work fine.

So, open up Form.cs in View Code mode, and just under the InitializeCompontent(); call, insert the following code:

public Form1() 

     Button b = new Button(); 
     b.Location = new Point(0, 900);
     b.Text = "test"; 

This will add a button 900 pixels down from the top of the innerPanel, and we should have a scrollbar showing up when we run the application.

Skinning the Scrollbar (If only it were that simple :p)

Now, we have a Panel that will scroll, but it's ugly. So, how do we skin it? Well, there is actually no way to skin a Windows scrollbar, as far as I know. So what we have to do is create our own scrollbar as a user control. Basically, we have to mimic exactly what the Windows VScroll control does, but add the ability to be able to use graphics for the arrows, channel, and thumb tracking controls. Then, later on, we will write some more code that will enable us to hide the panel scrollbar, as well as make our custom user control scrollbar, control the panel's scrolling.

We start by adding a new Control Library Project to our solution, and we will rename the User Control that it adds by default, to CustomScrollbar.

Now, we start creating graphics and writing code to make our CustomScrollbar look and function the way we would like it to.

Creating the Scrollbar Graphics

So first, we have to figure out how to create graphics in a fashion that lends itself to being reusable in most instances. I know that a scrollbar always has four common properties:

  • Up arrow
  • Down arrow
  • Thumb control
  • Channel

This is cool, so we just need the ability to be able to supply an up arrow graphic, down arrow graphic, thumb control graphic, and a channel graphic/color.

However, there is one more thing.... The thumb control for a standard Windows scrollbar always sizes appropriately depending on the Minimum/Maximum and LargeChange properties. This means that we have to give our thumb control the ability to change its size dynamically. Yes, this is not fun, because in order to do this using graphics, we have to split up our thumb control graphic into five different graphics, two of which will need to be spanned accordingly to how big our thumb control is supposed to be. Here is how I cut up my graphics:

Sample Image - customscrollbar3.gif

So as you can see, we will have the following graphics which we will allow to be customized through the Properties panel on the user control.

  1. Up arrow image
  2. Down arrow image
  3. Thumb top image
  4. Thumb top span image
  5. Thumb middle image
  6. Thumb bottom span image
  7. Thumb bottom image

For this implementation, I will implement the channel portion as a solid color rather than as a graphic, just for demonstration. It's easy enough to change it to use an image if you like. Incidentally, it is also very easy to add the ability to have mouseover images for the arrows and thumb controls, but I won't be covering that. I will leave that as an exercise for all you developers out there :)

Preparing the properties and events for the scrollbar control

Now, we have our graphics all figured out. We just have to determine how we want our scrollbar to function. I don't like re-inventing the wheel or changing the things that I am used to. So, I am going to make our custom scrollbar work exactly like the VScroll or Windows scrollbar control, so that if I want to use my custom scrollbar in place of an ugly Windows scrollbar, I can just simply swap them out and it will work.

Therefore, my control will have the following properties exposed:

  • Maximum - int
  • Minimum - int
  • Value - int
  • LargeChange - int
  • SmallChange - int

and the following events:

  • Scroll
  • ValueChanged

and the following custom properties to facilitate our ability to skin:

  • ChannelColor - Color
  • DownArrowImage - Image
  • ThumbBottomImage - Image
  • ThumbBottomSpanImage - Image
  • ThumbMiddleImage - Image
  • ThumbTopImage - Image
  • ThumbTopSpanImage - Image
  • UpArrowImage - Image

With all of these properties implemented properly in a user control, we will have ourselves a completely customizable scrollbar that will function exactly as a regular Windows scrollbar, but it will also have the ability to look as cool or uncool as we would like it :)

Implementing the Scrollbar User Control

Now, here is the tough part. We have to program our very own scrollbar control. This is not necessarily an easy task to accomplish. However, once you are done making this work, you should never have to code another one ever again, and if you do, at least you will have a code base to start from with what we have developed here.

In order to make our custom scrollbar, we will need to override and/or respond to the following events:

  • OnPaint - override
  • MouseUp - handle
  • MouseDown - handle
  • MouseMove - handle

First, we will instantiate all of our variables. Some are protected, and will later be exposed, and some are private for internal use only.

//Our channel color that we will expose later
protected Color moChannelColor = Color.Empty;

//Our images for the scrollbar that we will expose later 
protected Image moUpArrowImage = null;
protected Image moDownArrowImage = null;
protected Image moThumbArrowImage = null;
protected Image moThumbTopImage = null;
protected Image moThumbTopSpanImage = null;
protected Image moThumbBottomImage = null;
protected Image moThumbBottomSpanImage = null;
protected Image moThumbMiddleImage = null;

//Our properties that we will expose later 

protected int moLargeChange = 10;
protected int moSmallChange = 1;
protected int moMinimum = 0;
protected int moMaximum = 100;
protected int moValue = 0;

//Our private variables for internal use 
private int nClickPoint;
private int moThumbTop = 0; 
private bool moThumbDown = false;
private bool moThumbDragging = false;
//Our public events that we are exposing

public new event EventHandler Scroll = null;
public event EventHandler ValueChanged = null;

Now that we have all our of variables setup, we need to expose a bunch of them as properties, so that developers that are using the scrollbar control can access and modify the properties at design time.

[EditorBrowsable(EditorBrowsableState.Always), Browsable(true), 
                 DefaultValue(false), Category("Behavior"), 
public int LargeChange {
    get { return moLargeChange; }
    set { moLargeChange = value;

[EditorBrowsable(EditorBrowsableState.Always), Browsable(true), 
                 DefaultValue(false), Category("Behavior"), 
public int SmallChange {
    get { return moSmallChange; }
    set { moSmallChange = value;

[EditorBrowsable(EditorBrowsableState.Always), Browsable(true), 
                 DefaultValue(false), Category("Behavior"), 
public int Minimum {
    get { return moMinimum; }
    set { moMinimum = value;

[EditorBrowsable(EditorBrowsableState.Always), Browsable(true), 
                 DefaultValue(false), Category("Behavior"), 
public int Maximum {
    get { return moMaximum; }
    set { moMaximum = value;

[EditorBrowsable(EditorBrowsableState.Always), Browsable(true), 
                 DefaultValue(false), Category("Behavior"), 
public int Value {
    get { return moValue; }
    set { moValue = value;

[EditorBrowsable(EditorBrowsableState.Always), Browsable(true), 
                 DefaultValue(false), Category("Skin"), 
                 Description("Channel Color")]
public Color ChannelColor
    get { return moChannelColor; }
    set { moChannelColor = value; }

[EditorBrowsable(EditorBrowsableState.Always), Browsable(true), 
                 DefaultValue(false), Category("Skin"), 
                 Description("Up Arrow Graphic")]
public Image UpArrowImage {
    get { return moUpArrowImage; }
    set { moUpArrowImage = value; }

[EditorBrowsable(EditorBrowsableState.Always), Browsable(true), 
                 DefaultValue(false), Category("Skin"), 
                 Description("Down Arrow Graphic")]
public Image DownArrowImage {
    get { return moDownArrowImage; }
    set { moDownArrowImage = value; }

[EditorBrowsable(EditorBrowsableState.Always), Browsable(true), 
                 DefaultValue(false), Category("Skin"), 
                 Description("Thumb Top Graphic")]
public Image ThumbTopImage {
    get { return moThumbTopImage; }
    set { moThumbTopImage = value; }

[EditorBrowsable(EditorBrowsableState.Always), Browsable(true), 
                 DefaultValue(false), Category("Skin"), 
                 Description("Thumb Top Span Graphic")]
public Image ThumbTopSpanImage {
    get { return moThumbTopSpanImage; }
    set { moThumbTopSpanImage = value; }

[EditorBrowsable(EditorBrowsableState.Always), Browsable(true), 
                 DefaultValue(false), Category("Skin"), 
                 Description("Thumb Bottom Graphic")]
public Image ThumbBottomImage {
    get { return moThumbBottomImage; }
    set { moThumbBottomImage = value; }

[EditorBrowsable(EditorBrowsableState.Always), Browsable(true), 
                 DefaultValue(false), Category("Skin"), 
                 Description("Thumb Bottom Arrow Graphic")]
public Image ThumbBottomSpanImage {
    get { return moThumbBottomSpanImage; }
    set { moThumbBottomSpanImage = value; }

[EditorBrowsable(EditorBrowsableState.Always), Browsable(true), 
                 DefaultValue(false), Category("Skin"), 
                 Description("Thumb Arrow Graphic")]
public Image ThumbMiddleImage {
    get { return moThumbMiddleImage; }
    set { moThumbMiddleImage = value; }

Now that all of our properties are exposed, let's start writing code to display our scrollbar. We will override the OnPaint of our user control to accomplish this task, as shown below:

protected override void OnPaint(PaintEventArgs e) {
    //set the mode to nearest neighbour so when
    //we span our thumb graphics it doesn't try 
    //to blur or antialias anything
    e.Graphics.InterpolationMode = 
    //draw up arrow
    if (UpArrowImage != null) {
        e.Graphics.DrawImage(UpArrowImage, new Rectangle(new Point(0,0), 
          new Size(this.Width, UpArrowImage.Height)));

    Brush oChannelBrush = new SolidBrush(moChannelColor);
    Brush oWhiteBrush = new SolidBrush(Color.FromArgb(255,255,255));

    //draw channel left and right border colors
    e.Graphics.FillRectangle(oWhiteBrush, new Rectangle(0, 
                             UpArrowImage.Height, 1, 
    e.Graphics.FillRectangle(oWhiteBrush, new Rectangle(this.Width-1, 
               UpArrowImage.Height, 1, 
               (this.Height - DownArrowImage.Height)));

    //draw channel
    e.Graphics.FillRectangle(oChannelBrush, new Rectangle(1, 
                             UpArrowImage.Height, this.Width - 2, 
                             (this.Height - DownArrowImage.Height)));
    //draw thumb contrl
    int nTrackHeight = (this.Height - 
                       (UpArrowImage.Height + 
    float fThumbHeight = ((float)LargeChange / (float)Maximum) * nTrackHeight; 
    int nThumbHeight = (int)fThumbHeight;
    if (nThumbHeight > nTrackHeight) {
        nThumbHeight = nTrackHeight;
        fThumbHeight = nTrackHeight;
    if (nThumbHeight < 56) {
        nThumbHeight = 56;
        fThumbHeight = 56;
    float fSpanHeight = (fThumbHeight - (ThumbMiddleImage.Height + 
          ThumbTopImage.Height + ThumbBottomImage.Height)) / 2.0f;
    int nSpanHeight = (int)fSpanHeight;
    int nTop = moThumbTop;
    nTop += UpArrowImage.Height;
    //draw top part of thumb now
    e.Graphics.DrawImage(ThumbTopImage, new Rectangle(1, nTop, 
                       this.Width - 2, ThumbTopImage.Height));
    nTop += ThumbTopImage.Height;

    //draw top span thumb
    Rectangle rect = new Rectangle(1, nTop, 
                         this.Width - 2, nSpanHeight);
    e.Graphics.DrawImage(ThumbTopSpanImage, 1.0f,(float)nTop, 
                        (float) fSpanHeight*2); nTop += nSpanHeight; 
    //draw middle part of thumb
    e.Graphics.DrawImage(ThumbMiddleImage, new Rectangle(1, nTop, 
                       this.Width - 2, ThumbMiddleImage.Height));

    nTop += ThumbMiddleImage.Height;
    //draw botom span thumb
    rect = new Rectangle(1, nTop, this.Width - 2, nSpanHeight*2);
    e.Graphics.DrawImage(ThumbBottomSpanImage, rect);
    nTop += nSpanHeight;
    //draw bottom part of thumb
               new Rectangle(1, nTop, this.Width - 2, nSpanHeight));

    //draw down arrow
    if (DownArrowImage != null) {
    e.Graphics.DrawImage(DownArrowImage, new Rectangle(new Point(0, 
             ( this.Height-DownArrowImage.Height)), 
             new Size(this.Width, DownArrowImage.Height)));

Now we have to write all the code to handle when a user clicks the up or down arrows, and the most difficult part, write the code to move the thumb control when the user clicks and drags the thumb portion of the control.

private void CustomScrollbar_MouseDown(object sender, MouseEventArgs e) {
    Point ptPoint = this.PointToClient(Cursor.Position);
    int nTrackHeight = (this.Height - (UpArrowImage.Height + 
    int nThumbHeight = GetThumbHeight();
    int nTop = moThumbTop;
    nTop += UpArrowImage.Height;

    Rectangle thumbrect = new Rectangle(new Point(1, nTop), 
              new Size(ThumbMiddleImage.Width, nThumbHeight));
    if (thumbrect.Contains(ptPoint))
    //hit the thumb
    nClickPoint = (ptPoint.Y - nTop);
    this.moThumbDown = true;
    Rectangle uparrowrect = new Rectangle(new Point(1, 0), 
       new Size(UpArrowImage.Width, UpArrowImage.Height));
    if (uparrowrect.Contains(ptPoint))
        int nRealRange = (Maximum - Minimum)-LargeChange;
        int nPixelRange = (nTrackHeight - nThumbHeight);
        if (nRealRange > 0)
            if (nPixelRange > 0)
                if ((moThumbTop - SmallChange) < 0)
                    moThumbTop = 0;
                    moThumbTop -= SmallChange;
                //figure out value
                float fPerc = (float)moThumbTop / (float)nPixelRange;
                float fValue = fPerc * (Maximum - LargeChange);

                moValue = (int)fValue;
                if (ValueChanged != null)
                ValueChanged(this, new EventArgs());
                if (Scroll != null)
                Scroll(this, new EventArgs());
    Rectangle downarrowrect = new Rectangle(new Point(1, 
              new Size(UpArrowImage.Width, UpArrowImage.Height));
    if (downarrowrect.Contains(ptPoint))
        int nRealRange = (Maximum - Minimum) - LargeChange;
        int nPixelRange = (nTrackHeight - nThumbHeight);
        if (nRealRange > 0)
            if (nPixelRange > 0)
                if ((moThumbTop + SmallChange) > nPixelRange)
                    moThumbTop = nPixelRange;
                    moThumbTop += SmallChange;
                //figure out value
                float fPerc = (float)moThumbTop / (float)nPixelRange;
                float fValue = fPerc * (Maximum-LargeChange);

                moValue = (int)fValue;
                if (ValueChanged != null)
                ValueChanged(this, new EventArgs());
                if (Scroll != null)
                Scroll(this, new EventArgs());

private void CustomScrollbar_MouseUp(object sender, MouseEventArgs e) {
    this.moThumbDown = false;
    this.moThumbDragging = false;

private void MoveThumb(int y) {
    int nRealRange = Maximum - Minimum;
    int nTrackHeight = (this.Height - (UpArrowImage.Height + 
    int nThumbHeight = GetThumbHeight();
    int nSpot = nClickPoint;
    int nPixelRange = (nTrackHeight - nThumbHeight);
    if (moThumbDown && nRealRange > 0) {
        if (nPixelRange > 0) {
            int nNewThumbTop = y - (UpArrowImage.Height+nSpot);

                moThumbTop = nNewThumbTop = 0;
            else if(nNewThumbTop > nPixelRange)
                moThumbTop = nNewThumbTop = nPixelRange;
            else {
                moThumbTop = y - (UpArrowImage.Height + nSpot);

            //figure out value
            float fPerc = (float)moThumbTop / (float)nPixelRange;
            float fValue = fPerc * (Maximum-LargeChange);
            moValue = (int)fValue;

private void CustomScrollbar_MouseMove(object sender, MouseEventArgs e) {
    if(moThumbDown == true)
        this.moThumbDragging = true;
    if (this.moThumbDragging) {
    if(ValueChanged != null)
        ValueChanged(this, new EventArgs());
    if(Scroll != null)
        Scroll(this, new EventArgs());

Now we are done writing the scrollbar user control. Keep in mind, I have left certain things out that are not that important. So please see the source code accompanied with this article. Now, we just need to the hide the default scrollbar on the panel, and hook up our custom scrollbar.

Hide the Default Scrollbar and Hook Up our Custom Scrollbar

So now, we have to add our custom scrollbar to our form, beside our Panels. So first, build our solution. Now, let's right click on our toobox and click "Choose Items..." and browse to the CustomControls.dll and add our CustomScrollbar to the toolbox.

Next, add an instance of our CustomScrollbar to the form beside the Panel. Now, we will add the code to hook up the CustomScrollbar so that it will scroll our panel that we setup.

Add the code shown heren just below the code that we added earlier to create the button, below the InitializeCompontent() function:

Point pt = new Point(this.innerPanel.AutoScrollPosition.X, 
this.customScrollbar1.Minimum = 0;
this.customScrollbar1.Maximum = this.innerPanel.DisplayRectangle.Height;
this.customScrollbar1.LargeChange = customScrollbar1.Maximum / 
             customScrollbar1.Height + this.innerPanel.Height;
this.customScrollbar1.SmallChange = 15;
this.customScrollbar1.Value = Math.Abs(this.innerPanel.AutoScrollPosition.Y);

Next, we implement the Scroll event from our CustomScrollbar control, and add the following code:

private void customScrollbar1_Scroll(object sender, EventArgs e)
    innerPanel.AutoScrollPosition = new Point(0, 

Now, the only thing left is to hide the default scroll bar that the Panel brings up. Well, that's actually very easy now, thanks to Panels. Remember the outerPanel that we created? Well, all we have to do is decrease the width of the outerPanel so that it hides the innerPanel scrollbar, and that's it.


So now, our custom scrollbar will scroll our Panel just as the regular scrollbar does. Only now are we able to change the look and feel of our scrollbar whenever we want. I did skip over some small details in the creation of this control, but all the code is available in the source code package above.

Some things to improve are as follows:

  • Add ability to support mouse rollover images for the up and down arrows and the thumb control.
  • Add ability to hold the mouse button down on arrows and have the control continually scroll.
  • Add ability to click the channel portion of the scrollbar and have it scroll the amount of the LargeChange property.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Web Developer
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

QuestionHow make it work? Pin
Member 1227577518-Feb-22 14:52
MemberMember 1227577518-Feb-22 14:52 
QuestionVery nice article Pin
manonthecorner_14-Dec-19 1:01
Membermanonthecorner_14-Dec-19 1:01 
QuestionHow to make it work on a listView? Pin
bascurtiz1-Oct-19 10:54
Memberbascurtiz1-Oct-19 10:54 
QuestionThank you! Pin
NikolayHidalgoDiaz29-Sep-18 16:19
MemberNikolayHidalgoDiaz29-Sep-18 16:19 
Questionthe onpaint funciont confuse me!! Pin
Member 1379310027-Apr-18 8:54
MemberMember 1379310027-Apr-18 8:54 
GeneralMy vote of 3 Pin
chirag solanki 9085110-Oct-13 6:31
Memberchirag solanki 9085110-Oct-13 6:31 
GeneralMy vote of 5 Pin
pjaar8920-Sep-13 1:23
Memberpjaar8920-Sep-13 1:23 
QuestionMouseMove in DatagridView with different RowHeights Pin
Member 97145177-Mar-13 20:01
MemberMember 97145177-Mar-13 20:01 
GeneralMy vote of 2 Pin
Lee P Richardson23-Aug-12 20:24
MemberLee P Richardson23-Aug-12 20:24 
Questiongreat work Pin
iosuntrichard20-Jul-12 4:37
Memberiosuntrichard20-Jul-12 4:37 
GeneralMy vote of 5 Pin
pcs041410-May-12 23:24
Memberpcs041410-May-12 23:24 
QuestionIs there is an example for ASP web control panel and scrollbar? Pin
ASPCoder77712-Jul-11 7:15
MemberASPCoder77712-Jul-11 7:15 
GeneralMy vote of 5 Pin
boryakov19-May-11 4:52
Memberboryakov19-May-11 4:52 
GeneralMy vote of 5 Pin
mike_tdc22-Dec-10 9:06
Membermike_tdc22-Dec-10 9:06 
Questionhow to use with rich text box? Pin
gt19878-Jun-10 4:41
Membergt19878-Jun-10 4:41 
QuestionThe scroll bar does not work if the child control is placed at a location greater than 20 times the height of the parent control Pin
Member 323180210-Jan-10 21:52
MemberMember 323180210-Jan-10 21:52 
QuestionHow to use HScrollBar control in C#? Pin
denotation25-Sep-09 22:51
Memberdenotation25-Sep-09 22:51 
GeneralInserting the Custom Scroll Bar into the Treeview Pin
Zap-Man24-Jul-09 11:41
MemberZap-Man24-Jul-09 11:41 
QuestionHorizontal version Pin
tanioskahi14-Jan-09 21:33
Membertanioskahi14-Jan-09 21:33 
GeneralBig Buttons Pin
Stanhope9-Dec-08 0:43
MemberStanhope9-Dec-08 0:43 
GeneralHi Pin
varundj1-Oct-08 15:54
Membervarundj1-Oct-08 15:54 
QuestionCustom scrollbar in a gridview Pin
AngelaM24-Sep-08 21:02
MemberAngelaM24-Sep-08 21:02 
QuestionTo change the color of scroll bar in a panel Pin
AngelaM28-Aug-08 23:02
MemberAngelaM28-Aug-08 23:02 
Questionthumb height Pin
SignF3-Jul-08 23:15
MemberSignF3-Jul-08 23:15 
QuestionScoll on mouse move Pin
Sajal Mahajan2-Apr-08 4:13
MemberSajal Mahajan2-Apr-08 4:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.