Wednesday, July 27, 2011

Create Slider Control With Custom Color

All too often I see examples of custom controls which unnecessarily add child controls. For instance, a custom trackbar control may have a label or panel added to act as the scrolling thumb tab.
In this instance that's not so bad as it only creates one extra window handle, but where several such child controls are added the user will see what is percieved as flicker as each window is individually painted one after the other. No amount of doublebuffering will cure this.

Since we need to respond to mouse, keyboard or code change events and then calculate the exact bounds of the child control, it really makes no sense to add the extra control. We may as well apply those bounds to a rectangle instead, and then paint within this rectangle (don't have to paint a Rectangle just need to paint inside it).

The following example shows how to define and use a rectangle for this purpose. I have tried to make it behave just like a standard System.Windows.Forms.Trackbar control, but have made no offort to have it look like one. Hopefully, you'll be able to apply the same principles to other custom controls, perhaps using some of the many control renderers to simulate "lite" controls.

VB net Sample:



CSharp Sample:



by: Mick Dohertys'

1 comment:

  1. Hi hope someone can help.

    Using VB.net in Visual Studio 2019

    When I try to add this to a form I get an error saying Type 'TrackBar.TrackBar' is not defined.

    When I click on the error it takes me to Me.TrackBar3 = New TrackBar.TrackBar().

    Deleting TrackBar. gets it working again. Does anyone know what in the code is causing this?

    ReplyDelete