I have followed a few tutorials on creating a custom property editor dialog, but there are so many things involved that I could not get it to work right. What I am trying to accomplish is a custom form with a date picker (calendar), a time picker, and OK and Cancel buttons. The form is no problem at all, but how would I go about implementing this so that I can publish a property in any component of a certain type with a button to launch the property editor?
I would like to completely override the TDateTime
type and put my custom editor in its place, so wherever a TDateTime
is published and visible in the Object Inspector, I can use this editor to modify both date and time together in the same window.
The problem is that the documentation on creating a custom property editor is poor and although some resources are very thorough, they go into too much detail of the capabilities and lack getting to the point on the most common scenarios.
I did not want to ask this question here and expect anyone to answer it for me, so I did the research myself to solve my issues and I would like to share the unique experience involved in this mini project, as I'm sure others are frustrated with the same thing.
There are many different possibilities with custom property editors, dialogs, and component editors. This in particular would call for a
TDateTimeProperty
descendant. This would allow you to be able to edit the value of the property directly in the Object Inspector as plain text (String) while keeping the DateTime formatting.I am assuming that you already have a general knowledge of creating custom components and a package in which you can publish this property editor from, because that's a lesson in its own which I will not cover. This calls for just one line of code to be placed inside the
Register
procedure, but we'll get to that later.First, you need to create a new form in your
Design-Time
package, where your components are registered. Name the unitDateTimeProperty.pas
, and name the formDateTimeDialog
(thus making the form's classTDateTimeDialog
). Place whatever controls you need, in this case aTMonthCalendar
,TDateTimePicker
(withKind
set todtkTime
), and 2TBitBtn
controls, one labeledOK
withModalResult
ofmrOK
and the other labeledCancel
withModalResult
ofmrCancel
.Your unit should look something like this:
And here is the
DFM
code behind this form:Now, add
DesignEditors
andDesignIntf
to youruses
clause. Make sure you haveDesignIDE
declared in theRequires
of thisDesign-Time
package. This is required for publishing any property editors.In the form, create a new public property called
DateTime
of typeTDateTime
with a property getter and setter. This property will allow you to easily read/write the fullTDateTime
value the selection actually represents. So you should have this in your form:Next we need to add the actual property editor class. Create this class just beneath the
{$R *.dfm}
which is just underimplementation
:Finally, we need to add a
Register
procedure to perform the actual registration of this new property editor:Now there's an important piece to understand in this call to
RegisterPropertyEditor
. Since the 2nd and 3rd parameters arenil
and an empty string, this means the editor will apply to all instances ofTDateTime
. Look into this procedure for more information about making it specific to certain components and property instances.And here's the final result after installing...
Some good resources for custom property editors which contributed are as follows: