Question

In a Delphi Form, I would like to replace one visual component with another. Example: I want to replace a Panel component with an ElPanel from a 3rd party package.

I would like all identical properties and events of the first component to be transferred to the new one, and all the components that belong to the first component (e.g. Toolbars, memos,status bars, etc.) to end up placed on the new component exactly where they were on the first one.

Is there a best/easiest way to do this other than adding the new component to the form and meticulously transferring every property, event and component to it one-by-one?

Was it helpful?

Solution

I do it as following:

  1. Right click on the form and choose (View as Text).
  2. Press Ctrl + F to search for the Component class name like TPanel and replace it with TElPanel
  3. Switch back to back to the form (View as form)
  4. In the interface section replace the TPanel with TElPanel.

if you have many components using GExperts will be more feasible solutions.

OTHER TIPS

You can use GExperts or you can do it by hand.

To do it by hand, open the .dfm in notepad and replace all the class names. (Replace TPanel with TElPanel for example). When you've made all your changes, open the .pas file with Notepad, and do the same thing.

Make sure you add the required units to your uses clause.

Then open the form in the IDE and clean up any mismatched events or unknown property problems.

If I recall the excellent free GExperts plugin does this. Right click your form and select "Replace Components". http://www.gexperts.org

IMHO, the big drawback of the Replace component GExpert is that it changes the order of the components in the source code. That is not very VCS friendly. :-)

If you have other components inside a container doing this replacement with GExperts will cause some ugly exceptions and possibly unexpected behaviour in the IDE.
So, the best solution is to edit the .dfm file where you want (inside or outside the IDE) and replace manually the types of the components that you want to change. Maybe it will cause some exceptions too, but the IDE will managed them.
If you do it inside the IDE, after switching to design view if you save the .dfm inmediately the IDE will ask you to change the type of the variables related to the components you touched, liberating you to do it.

To convert between text and binary dfm formats, use the convert.exe tool in the Delphi bin\ directory. – Tim Knipe (Oct 28 at 4:15)

You can also use the context menu of the form designer - at least with BDS 2006. It's the last menu item ("Text-DFM" in a German IDE).

In my project I had to convert few doezens forms from one set of components to another. I have created small perl script to convert components and its properties and do all neccesary mapings. The script is quick&dirty solution but it is highly configurable. It scanns all dfm and pas files in project direcotory and convert dfm components definitions according to rules that you should provide in ObjectBeginFound, PropertyFound, ObjectEndFound procedures/events.

DFM files should be in text mode. Tested on Delphi 5 files. I don't know if it will be compatible with newer versions. Please send posts if you find it out.

USAGE: perl.exe cxdfm.pl > logfile.txt

DOWNLOAD LINK http://dl.dropbox.com/u/15887789/cxdfm.pl

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top