Click here to Skip to main content
15,875,581 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I create (C#) a custom radio button based on the default Windows Forms RadioButton. I paint it in the overridden OnPaint method.

Solution 1: a classic one (I think) is to create (allocate memory) brushes or pens within the OnPaint method, paint stuff and dispose them at the end of the method.

Solution 2: how about creating the brushes only once, as the static members of the class, share them among all instances of the radio button and dispose them by the end of the application life (or maybe even just wait for the application end without explicit disposing).

This is for a small tool-like application, a user would run it, choose an option by a radio button and close the app.

Practically, in both mentioned solutions, the brushes exist all the time the application lives. In solution 1 the brushes are being created and disposed all over again, unnecessary(?), so would static solution 2 be a better idea?

regards
ROB
Posted

1 solution

If you aren't changing the brushes at all (or even if you aren't changing them between most paint events) then I'd create them as a single object and reuse them: it's quicker, and that can count a lot in a user interface.

Static? Dunno - I'd probably make them class level, unless there was a good reason for making them common to all instances of the control across the whole application - remember that if the control is used in two different forms they would still share the same static objects.
 
Share this answer
 

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