Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to use a simple property editor (2) Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
31-May-03
Category
VCL-General
Language
Delphi All Versions
Views
78
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Daniel Wischnewski 

Sometimes, when you create components, you want to make it easier for the 
developers to use them. This is the time to develop a property editor.

Answer:

INTRODUCTION 

In this article I will give you a short introduction to property editor 
development. This property editor developed here will simlpy allow you to edit 
string and TCaption properties in a better way, allowing you to add line breaks to 
strings. 

There are two reasons for this property editor. First it is great to add line 
breaks into the labels caption, second it is fairly simple, therefore a good start 
for developing a property editor. 

STEPS IN CREATING A PROPERTY EDITOR 

First a short list of considerations when creating a property editor. 

How should the property editor support the developer? 
Which components/properties/data types should the editor support? 
When do you have enough time to write it? :) 

How do we support the developer? 

Well, as I have written before, we will give the developer a simple way of adding 
line breaks to. The form we will create with the Delphi form designer, jsut as we 
do always. We add a public procedure to it, that will take the old value, load it 
into a memo field, show the form and return the either new value or the old if the 
user has not confirmed the changes. 

Which components/properties/data are supported? 

We will support all components and properties of the types string and TCaption. 

DESIGING THE FORM 

Start Delphi and close all open files. Create a new form and name it 
frmStringEditor. Add a memo field to the form and name it mmoStringProperty. Now we 
need to buttons, one for "OK" and one for "Cancel." Thats all for the design part. 
Make it fit "nicely." Add event handlers to the to button click procedures! 

We will add one public procedure that will accomplish the form show and decide, 
whether the property is changed or not. 

function Edit(var Data: string): Boolean;

The remaining code comes a little later. 

CREATING THE PROPERTY EDITOR CLASS 

All Property Editors have to be a descendend of the TPropertyEditor class. In our 
case we will descend from the TStringProperty class, that itself descends from the 
one previously named. 

There are two function we need to override. GetAttributes to tell Delphi that we 
provide a dialog to manipulate the property. Edit is the function called when the 
developer calls for the property editor dialog. 

1   TOurStringProperty = class(TStringProperty)
2   public
3     function GetAttributes: TPropertyAttributes; override;
4     procedure Edit; override;
5   end;


The remaining code comes a little later, too. 

REGISTERING THE PROPERTY EDITOR 

We will install the property editor just like we install components, therefore we 
have to provide the Register property. In the body we will add a call to the 
RegisterPropertyEditor function. This function takes four parameters. 

Information about the property type handled by the editor 
The component/control class this editor is for (nil for all) 
The property this editor is for ('' for all) 
The property editor class itself 

AND NOW THE WHOLE CODE 

I have placed this all into one unit developed on Delphi 5 and tested with Delphi 6 
Evaluation version. You will need at least the Professional Editions to get it 
working. Earlier versions of Delphi should work just fine. Cannot test on them, 
sorry. 

This unit assumes that you saved your form under the name of uStringEditor. 
6   
7   unit uStringEditor;
8   
9   interface
10  
11  uses
12    Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
13    StdCtrls, TypInfo
14  {$IFNDEF VER140}
15    , DsgnIntf
16  {$ELSE}
17    , DesignEditors, DesignIntf
18  {$ENDIF}
19    ;
20  
21  type
22    TfrmStringEditor = class(TForm)
23      mmoStringProperty: TMemo;
24      btnOK: TButton;
25      btnCancel: TButton;
26      procedure btnOKClick(Sender: TObject);
27      procedure btnCancelClick(Sender: TObject);
28    private
29    public
30      function Edit(var Data: string): Boolean;
31    end;
32  
33    TOurStringProperty = class(TStringProperty)
34    public
35      function GetAttributes: TPropertyAttributes; override;
36      procedure Edit; override;
37    end;
38  
39  procedure register;
40  
41  implementation
42  
43  {$R *.DFM}
44  
45  procedure register;
46  begin
47    RegisterPropertyEditor(TypeInfo(TCaption), nil, '', TOurStringProperty);
48    RegisterPropertyEditor(TypeInfo(string), nil, '', TOurStringProperty);
49  end;
50  
51  function EditConnectionString(
52    Component: TComponent; PropInfo: PPropInfo
53    ): Boolean;
54  var
55    Str: string;
56  begin
57    Result := False;
58    with TfrmStringEditor.Create(Application) do
59    try
60      Caption := Format('%s.%s string editor', [Component.Name, PropInfo^.Name]);
61      Str := GetStrProp(Component, PropInfo);
62      if Edit(Str) then
63      begin
64        SetStrProp(Component, PropInfo, Str);
65        Result := True;
66      end;
67    finally
68      Free;
69    end;
70  end;
71  
72  { TOurStringProperty }
73  
74  procedure TOurStringProperty.Edit;
75  begin
76    if EditConnectionString(GetComponent(0) as TComponent, GetPropInfo)
77      then
78      Modified;
79  end;
80  
81  function TOurStringProperty.GetAttributes: TPropertyAttributes;
82  begin
83    Result := [paDialog];
84  end;
85  
86  { TfrmStringEditor }
87  
88  procedure TfrmStringEditor.btnCancelClick(Sender: TObject);
89  begin
90    ModalResult := mrCancel;
91  end;
92  
93  procedure TfrmStringEditor.btnOKClick(Sender: TObject);
94  begin
95    ModalResult := mrOk;
96  end;
97  
98  function TfrmStringEditor.Edit(var Data: string): Boolean;
99  begin
100   mmoStringProperty.Text := Data;
101   if ShowModal = mrOK then
102   begin
103     Result := Data <> mmoStringProperty.Text;
104     Data := mmoStringProperty.Text;
105   end
106   else
107   begin
108     Result := False;
109   end;
110 end;
111 
112 end.


INSTALLING IT 

Go to the menu Component | Install Component..., select the your file from the disk and press "OK." After compiling and saving the package you are finished. You may have to restart Delphi for the changes to take place. 

			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC