After reading this question, I was wondering if it is possible to modify TCustomEdit
to check for the text width in a way that all of its descendants inherit the changes too?

- 1
- 1

- 4,674
- 1
- 38
- 83
-
Not without either modifying the VCL unit or do some serious hacking. If you need the modfied behaviour in design time as well, you will have to rebuild the VCL packages as well. – GolezTrol Sep 23 '12 at 08:53
-
@GolezTrol No changes in design time; a few methods and one new event – iMan Biglari Sep 23 '12 at 08:55
-
It's not exactly what you describe, but maybe this will help: http://stackoverflow.com/questions/9877718/changing-component-class-at-run-time-on-demand – GolezTrol Sep 23 '12 at 08:58
-
@GolezTrol I'm going to try it; the question is if I _patch_ `TCustomEdit`, will its descendants show the modified behavior? – iMan Biglari Sep 23 '12 at 09:04
-
I think they will, but I'm not sure. I would do a little test before going through the full modifications. ;-) – GolezTrol Sep 23 '12 at 09:13
-
@GolezTrol That's what I'm planning to do. – iMan Biglari Sep 23 '12 at 09:14
-
1You should reconsider the need to do this. Making changes to the VCL means that future updates (ones that patch or update files rather than reinstalling everything) will no longer work, because they won't consider the file to be valid. It also makes your code incompatible for other developers who don't have the same changed VCL source. It's much better just to make your own descendant of controls you need the new behavior on in the proper way, and use them instead. – Ken White Sep 23 '12 at 14:19
-
@KenWhite I'm looking for a way to introduce the functionality I need at runtime. By no means I will modify and recompile the VCL. And, if I were to make my own descendants, I had to create as many descendants as there are TCustomEdit's. – iMan Biglari Sep 23 '12 at 15:09
2 Answers
Most changes you make to TCustomEdit
will be inherited by descendants. That's generally how inheritance works. It specifically depends on what kind of changes you make, though:
If you edit StdCtrls.pas, then any changes you make will be inherited by any newly compiled code. One obstacle to this technique is getting Delphi accept the changes to your version of StdCtrls.pas without having to recompile other parts of the library that are difficult or impossible to recompile.
If you patch
TCustomEdit
methods at run time (by the usual technique of overwriting the first few bytes of the method to direct control to a method of your own), then those changes will also be inherited by descendants.If you patch the
TCustomEdit
virtual-method table, then some changes will be inherited, but not all. Virtual calls to your patched methods will use your custom version, but non-virtual calls will continue to use the original version. When a descendant usesinherited
, the dispatch of that call to the inherited method is not virtual, so the VMT is not involved, and the descendant will end up calling the originalTCustomEdit
method.

- 161,384
- 21
- 275
- 467
-
What I actually need to do is to patch `procedure Change; dynamic;`. Your answer clearly indicates that should I patch the VMT, the descendants which override `Change` will not be affected. `Change` is called in response to `CM_TEXTCHANGED`. So how to intercept this message at `TCustomEdit` level? Can class helpers be of any use in this matter? – iMan Biglari Sep 24 '12 at 19:24
-
In which part of my message do I clearly indicate what you should do? I didn't tell you what you should do at all. I told you what would happen if you did each of those things. If you don't like what would happen if you patched the VMT, then don't do that. Do something else instead, like patch the function itself. Or patch the message-handler function. Class helpers are irrelevant. – Rob Kennedy Sep 24 '12 at 19:29
-
I meant that your answer is clear on what happens with overridden methods _if_ I patch the VMT. – iMan Biglari Sep 24 '12 at 19:36
-
You're right! You wrote "should I," but I saw "I should," so I completely misinterpreted you. I'm sorry. – Rob Kennedy Sep 24 '12 at 19:55
-
Never mind... Now, could you please give me a few hints on how to use your second method? Patching `TCustomEdit` methods at runtime? – iMan Biglari Sep 25 '12 at 07:01
TCustomEdit is in stdctrls Unit. to test out what changes would be inherited, don't modify the one on the VCL path, copy stdctrls to your main project folder and modify it there. Delphi will find your modified unit first and will use it.
This leaves all other projects and VCL patches using/updating the original unit. Your patched unit would not be affected.
If I am right then modifying it even in the new location would cause a rebuild of the relevant VCL units. So when switching to other projects you will need to do a build so it recompiles with the VCL parts using the original unit in effect "switching back".

- 1,745
- 12
- 18