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?
I do it as following:
Right click on the form and choose (View as Text).
Press Ctrl + F to search for the Component class name like TPanel and replace it with TElPanel
Switch back to back to the form (View as form)
In the interface section replace the TPanel with TElPanel.
if you have many components using GExperts will be more feasible solutions.
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
Related
In the past, I have written custom components for Borland C++Builder. Some of these were derived from existing components, for example:
class PACKAGE TMyEdit : public TEdit
{
...
}
For some of these, I have also made custom bitmaps for the palette (.dcr files). I know how to create and edit them; that's not the point here.
Nowadays, I'm using a newer version of the IDE, Embarcadero C++Builder XE, and I notice a difference in behaviour for components without a .dcr file.
In the old days, the bitmap on the palette would be the same as the one where the component was derived from. Let's say the TMyEdit had the same bitmap as the TEdit.
But now, these components get a default icon. So all components without a .dcr file look alike on the palette.
I see 2 possible ways to solve this:
Is there perhaps some way to indicate in the source files that I want to inherit the bitmap of the original component?
If that isn't possible, I'll have to create new .dcr files for each new component. Is there a neat way to retrieve the original bitmaps? Perhaps there exists a resource file? Or an option in a wizard that I haven't found yet? If not, I'll have to resort to using Alt-PrtScn and MSPaint. That would work, but it would be tedious.
I have hundreds of components in my Delphi 2006 installation, but I always have trouble finding those I haven't used before and that may be the solution for what I need at the moment. I have many undocumented packages, but by the name of the component you can have an idea of what it does.
For example:
I need a RadioButton, so I have TRadioButton, TElRadioButton or TAdvRadioButton to choose which one has the custom properties that I need.
But when I click the Filter Button of the Tool Palette, I have to Type from the beginning of the component name. If I could have a field editor where I could type only a part of the name and the Palette showed me the components that had that term in the middle of the name too, I would make my day!!!
Does anyone know how in Delphi 2006?
I know that Delphi XE2 has this filter field.
The DDevExtensions IDE extension (http://andy.jgknet.de/blog/ide-tools/ddevextensions/) has a Component Selector that does what you need
(also using D2006, so it definitely works)
Different Delphi versions (in my case 2007, XE and XE2) seem to use the same code-template repository, such as ..\Documents\RAD Studio\code_templates. Since XE2 uses namespaced units, this causes problems when one have to use a unit name in a template.
For instance I have a template when I write winbeep which completes to windows.Beep(300, 100); and selects and stops on frequency etc.. This does not compile on XE2 unless I remove the Winapi. part of the Windows unit in the uses clause (or of course add the prefix manually). I have to have the unit name because otherwise SysUtils.Beep gets referred.
I couldn't find an option entry or a registry key or an environment variable specifying the place of the templates. It is not even related to BDSUSERDIR, in fact I don't even have a directory in my computer having the path of BDSUSERDIR (which is ..\Documents\RAD Studio\9.0). Is this my oversight and in fact there's an option to relocate code templates? If not, is it possible to make the IDE use unit aliases in uses clause for new forms?
edit:
Here it is stated that (emphasis by me) "Templates that you create (and templates provided by third party add-ins) are saved by default in the \My Documents\RAD Studio\code templates\ directory." That would imply there should be way to save them in a non-default place. But the article does not mention how.
You can put your templates in "Delphi installation folder"\ObjRepos\en\Code_Templates to make them visible only on one Delphi version.
Edit
As Sertac points out in the comments, Template libraries are related to project templates, not code templates. Leaving this answer in place because it may help people looking for ways to structure / share / split their project template repositories.
In Delphi XE2 you have a "Template libraries" option under the "Tools" menu. When you open that, you get this dialog:
The "Add" button allows you to add a folder, the "Properties" button allows you to view and manage the contents of that repository.
So it would seem to me that you can simply copy the template library, change it XE2 specific stuff, and then point XE2 to its own repository. Or you could do that only for the templates that require specific XE2 unit handling.
I have lots of components installed - trouble is when I want to find one I rarely use it takes ages to find it. I can look at the ALL choice which lists all components but finding one with say 'grid' in the title is still very time consuming. Is there a database of titles or (preferably) a text file I could interrogate and search?
Try DDevExtensions 1.61, it add ability to search for components for Delphi from version 5 and above, with other many improvement for the IDE.
Which version of Delphi? In recent versions, just click on the top of the palette and type "grid". Ctrl-Alt-P will focus the palette, and then you can just start typing.
In RAD2009 and RS2010, do this and it'll find all components with "grid" anywhere in the title.
Since you didn't specify which version of Delphi...
1) In older versions (D7 and before), you can right click on the component palette to see the properties. This shows all of the components you have installed one page at a time. It isn't searchable, but at least the list shows each component's name and package, so it's easier than using the popup hints on each individual component.
2) In Delphi 7, you can select View|Component List, which brings up a searchable alphabetical list of all components. Unfortunately, it only matches on the first part of the component name. The first version of the "new" tool palette had this same limitation.
3) DDevExtensions (free) allows partial string matches in the old component palette (D5-D7), and restores something like the old palette in newer versions of Delphi (D2005 and up). It's also included in Delphi 2010 for people who prefer that style. I like how the search works in Delphi 7, but in Delphi 2010, the filtering seems a little awkward to me. It filters the components and palette pages, but if you have matching components on more than one page, they aren't all shown at the same time. You still have to click on each individual page.
4) My favourite. I prefer the new tool palette (Delphi 2005 and up). Very configurable, lets me see the component names, shows components on more than one page at a time, dockable, etc.
Bottom line. If you don't have or don't like the new tool palette, get DDevExtensions.
Before Delphi 2005: Install DDevExtensions, then choose Tools -> DDevExtensions Options -> Component Selector -> Check the "Active" box, click OK
Delphi 2005 and later: Click in the component palette and type the name of the one you want.
Use GExperts - they help a lot.
I need to switch every control of a particular type on a form to a different type while maintaining the name and the code associated with each control.
For example, let's say I need to switch a dozen or more TEdit fields to TSpinEdits. How can I do that in Delphi 2007 with minimal effort?
Use the GExperts Replace Components expert. It's free, and is the only tool I've seen that does what you're asking, including mapping properties from the old component to the new one.
If you don't feel like using any external tools, you could always try switching to view the form as text, then search and replace TEdit to TSpinEdit. When you switch back and save the form, the IDE should tell you that the declarations in the .pas file are incorrect and offer to correct them automatically. Property mappings shouldn't be a problem unless there are properties that are named differently that you want to map from one class to the other. Properties that don't exist on the new class will be deleted. This is how it works in Delphi 7 anyway...
If you're not happy with the results, you might try GExperts as recommended by Craig, which are a great addition to the IDE for many reasons.
I use the CNTools component replacer (couldn't find the one in GExperts), it's got a good Component Selector feature too so you can select multiple controls based one some simple queries.