tags:

views:

163

answers:

4
+2  Q: 

Interposer class

Do I need to put my Interposer class in all the form that will the particular class ? Say I want to re-implement TPanel, I redeclare it as TPanel = class(ExtCtrls.TPanel) in the unit. Do I need to do this in all the unit that uses TPanel ?

Thanks in advance !

+3  A: 

If you want to design a new component, you'll have to give it a unique name. Otherwise the form designer and the serialization code won't be able to tell them apart. Call it TRoderickPanel or something. Then just replace all your TPanel objects with TRoderickPanel objects on every form that uses them. (GExperts adds a right-click option that makes this much easier.)

Mason Wheeler
Yes, I know what you mean. But I'm not really trying to design a component. What I'm trying to accomplish is sort of a hack just to satisfy a requirement of another software and communicate with each other. Specifically, my TLabels cannot be recognized by QTP. But TStaticText does. So I'm tricking QTP that my labels are indeed TStaticText. The interposer class will be sandwiched between a conditional define for a special build for QTP testing. And it work. Except for that I've got heaps of forms and I'm wondering whether is there a short cut so I don't need to redeclare it for each form.
Roderick
Ah, I see. In that case, no. The closest you can come is probably to put your type definition in an include file and include that in every form. But TLabel and TStaticText come from two different branches of the VCL tree. What you're trying to do isn't very type-safe, and is likely to lead to strange crashes if you're not very, very careful...
Mason Wheeler
+5  A: 

You yourself have acknowledged that this is a hack. It's not meant to scale well. It's meant for one-time cases where it's not worth the trouble to "do it right" by writing a bona fide custom control.

You can try putting your new class declaration in a separate unit. Make sure that unit appears on uses clauses after the VCL unit that declares the "real" version of the class. If that doesn't work, then yes, you need to make new declarations in every unit that uses the hack.

Since you're doing this to turn TLabel into TStaticText, you'd probably be better off simply changing your TLabel controls into real TStaticText controls on your forms. (That was the answer the last time you asked about this.) That way, the program you ship will be the same program you tested. Otherwise, you're testing a program with one kind of control and shipping one with another.

Rob Kennedy
+1  A: 

Depending on the functionality you are trying to add, you could consider using a class helper. If what you need cannot be done that way, I'd just bite the bullet and create a new class like Mason said. Replacing it right through your code is not that hard really, given that form files are also stored as text.

If you also have Visual Studio installed, that actually has a "Replace in files" command. On top of that I'm sure Google will be able to help you out with stand-alone tools. And I haven't used GExperts in a while, but I'm almost certain that had a "Replace component" command.

This is a far better solution for the long term. If you can use a class helper for what you need, that may be quicker, simpler and less work.

Cobus Kruger
The functionality he's trying to add is for his label controls to have window handles so his testing program can manipulate them externally at run time. Class helpers just introduce new methods into a class's scope. Replacing the components altogether is definitely the way to go.
Rob Kennedy
Yeah, I didn't see his description in the comment above. Class helpers won't work. I've replaced a couple of components like this before and believe it to be worth the effort.
Cobus Kruger
+1  A: 

I would instead use the GExperts wizard to replace components to get the behavior you are wanting. Doing what you are suggesting would require re-registration of the components so that the DFM loading mechanism created the proper components, and if I'm not mistaken, the component registry doesn't allow duplicates so would generate an exception. What your suggesting works well with classes, but not components used on a form.

skamradt