Click here to Skip to main content
15,885,032 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all,

This is driving me nuts. I am trying to label the ticks of a trackbar control. Therefore I want the control to use its base paint implementation, so I can add paint code to draw labels underneath every trackbar tick. I found an example that does basically the same thing. TimeSlider - A time-based TrackBar[^]

What I do is the following :

C#
public class DateTimeTrackBar : TrackBar
{
   :
   public DateTimeTrackBar()
   {
      :
      SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint |      ControlStyles.ResizeRedraw, true);
   }

   protected override void OnPaint(PaintEventArgs e)
   {
      base.OnPaint(e);

      base.SetStyle(ControlStyles.UserPaint, false);

      base.Refresh();                      // Use default paint implementation.

      // Adding additional paint code.
      if (ShowLabels)
         DrawLabels(e);                    // Works fine.

      base.SetStyle(ControlStyles.UserPaint, true);
   }
}


When the control comes up everything seems ok, but when I touches the trackbar thumb the control repaints itself and my labeling disappears. How is this possible ?

tia
Posted
Comments
Kornfeld Eliyahu Peter 24-Jan-16 6:59am    
What the debugger shows?
Dave Kreskowiak 24-Jan-16 10:45am    
Why are you calling .Refresh()?? That's a bad thing because inside the Paint event, you're telling the control to invalidate its client area and repaint itself AGAIN, before the control is done repainting itself. You're forcing the control to repaint itself CONTINUOUSLY!

On top of that, you do NOT use SetStyle inside the Paint event. SetStyle should be used one time only, when you create an instance of the control, preferably from inside the constructor of the control.
Ronny Portier 24-Jan-16 10:47am    
Yes, i am aware of this. Is there another way to make the control use its default paint implementation ?

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900