c# - Invalidate() custom control on every property change? -
i working on custom winforms control exposes lot of properties affect how control looks (different colors background, text , different states hover, click icons etc). change 1 of these properties makes repaint necessary @ moment every property has getter , setter setter calling invalidate(). extremely annoying code , if multiple properties changed @ once, every single 1 of them causes repaint though 1 repaint @ end enough.
i had following ideas don't know if possible or how implement them:
- cache property changes few milliseconds ,
invalidate() - use kind of reflection detect whether caller going change property in next line , if delay call
invalidate() - maybe attributes before properties make repaint necessary or list of such properties expanded getters/setters @ compile time
can give me advice if these ideas make sense/are possible , point me how implement them?
just note, stay away option 2. in regard option 1, in way property changes being cached system, redraw once ui thread has gone processing messages. option 3, seems little involved.
but in answer question, easiest way have work create timer in class fires off @ given interval. have bool if true timer's tick handler call invalidate() , set false. lastly make method use in place of invalidate mark bool true.
there may hazards not aware of in situation here example:
public class invalidationusercontrol : usercontrol { bool _isinvalid = false; int _data; timer t = new timer(); public invalidationusercontrol() { initializecomponent(); t.interval = 100; // go ahead play number, higher // greater latency t.tick += t_tick; t.start(); } void invalidateifneeded() { _isinvalid = true; } void t_tick(object sender, eventargs e) { if (_isinvalid) { _isinvalid = false; invalidate(); } } public int data { { return _data; } set { _data = value; invalidateifneeded(); } } } there may better ways out there quick solution may work way need to.
all have call invalidateifneeded() instead of invalidate() , should work.
Comments
Post a Comment