问题
I need to create and manage many simple published properties. I call them auto-properties if they look like that:
private
FTitle: string;
published
property Title: string read FTitle write FTitle;
Usually I create them next way:
Adding property name and type:
property Title: string
Selecting and copying property name
Title
to clipboard.Appending the line with
read F
Ctrl+Vwrite F
Ctrl+V;
- Pressing Ctrl+Shift+C and this will generate the private field
Is there any way to exclude steps 2 and 3 to add properties faster? Maybe it is possible to create some macro for this?
回答1:
Since Delphi 2006 you can use LiveTemplates.
In Delphi XE for example:
- type propf and hit Ctrl + J keystroke
- write the property name and hit TAB key
- write the property type name, hit TAB or ENTER key and you are done
You can find more information on how to write your own Live Templates here:
- Delphi Live Templates - http://delphi.wikia.com
- Creating Live Templates - http://docwiki.embarcadero.com
回答2:
I use macros for that purpose.
For example I have model with fields
private
FTitle: string;
FName: string
FAge: Integer
then I copy-paste the fields into published section and create macro
- Goto first field and hit Home
- Hit Ctrl + Shift + R to start recording macro
- Use Crtl + ->, Crtl + <- and End keys for navigation and convert first field to property like property Title: string Read FTitle Write FTitle;
- After that hit Home and go to next row
- Finish macro by hitting Ctrl + Shift + R
- For all other fields, just press Crtl + Shift + P
At first it seems difficult, but the skills will pay off.
回答3:
In XE7 type prom and hit enter. It seems faster.
来源:https://stackoverflow.com/questions/10816033/how-to-create-an-auto-property-faster-in-delphi-ide