views:

907

answers:

6

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?

+5  A: 

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

Osseta
+8  A: 

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.

Tim Knipe
Actually, you can't edit the .dfm file in notepad, since it is a binary file in later versions of Delphi.
lkessler
Actually, it is a binary file in /earlier/ versions of Delphi. In later versions it is a text file.To convert between text and binary dfm formats, use the convert.exe tool in the Delphi bin\ directory.
Tim Knipe
(Refer the "new forms as text" option under Tools|Options|VCL Designer)
Tim Knipe
It seems that either the default in Delphi 2009 is back to binary, or it may be that it retained the binary dfm format of Delphi 4 on my upgrade to Delphi 2009. Your tip about the "new forms as text" option is a good one.
lkessler
+4  A: 

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.

Mohammed Nasman
This is the best solution, without going for the GExperts plugin.
lkessler
Good that you like it ;-)
Mohammed Nasman
+1  A: 

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.

eKek0
A: 

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. :-)

Ulrich Gerhardt
A: 

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).

Ulrich Gerhardt