Click here to Skip to main content
15,892,269 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi,
I want to add tick labels to a trackbar control. Each tick has a label underneath it.
Therefore I do the following :


public DateTimeTrackBar()
{
    :
    SetStyle(ControlStyles.UserPaint  | ControlStyles.ResizeRedraw, true);
}


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

    if (ShowLabels)
        DrawLabels(e);
}


The label drawing works fine, except that the original trackbar disappears.
How can i keep the original trackbar drawing and extend it with my label drawing code ?

tia
Posted

1 solution

Instead of OnPaintBackground (its purpose is different), override OnPaint:
Control.OnPaint Method (PaintEventArgs) (System.Windows.Forms)[^].

Alternatively, you can handle the corresponding event System.Windows.Drawing.Control.Paint. But you don't need it because you are writing derived class anyway, so overriding is not a problem. By the way, if you override the method, don't handle this event (you could do it both, but usually it's poinless) and don't want the users to handle this event, don't call base.Paint.

—SA
 
Share this answer
 
Comments
[no name] 23-Jan-16 11:56am    
A 5. Especially for bringing (point out) OnPaintBackground back into my mind.
Sergey Alexandrovich Kryukov 23-Jan-16 11:57am    
Thank you, Bruno.
—SA

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