Control inheritance runtime question

Dec 3 2009 6:39 AM
Here is an interesting one for you all and i am sure i am right in this one but it is worth posing the question to everyone.

I have a bog standard form with say a button control on it. My library contains a customized button control which inherits from 'Button' but has a few extra properties and overrides certain methods like the 'Paint'.

Now then at runtime, i have a button on my form which i would like to replace with my customized version. Thus making the functionality of being able to design a form in the visual designer as you normally would and then at runtime you would get the customized version of it.

Now one idea would be to go through the form, find the instance of the button and replace it with my customized version of it. This is fine but i would imagine that the initial find/replace would be a bit hefty and would impact user experience.
The preferred way would to be take the button at runtime and instead of using the 'System.Windows.Forms.Button' class,  it would inherit/use the  'MyLib.Controls.Button' class.

I appreciate that classes are compiled and cannot be changed but i have seen things like skinning dll's that you just add into your project and they do this all at runtime. Is this simply just a find and replace, are they doing something clever with the API, or are they able to do what i am describing at runtime?

If anyone has any ideas, that would be great. I am however willing to accept that i may just have to do a find and replace at runtime. Bit of a pain and slow but at least i would only have to do it once.

Cheers



Answers (1)