Delphi32.com - Home!
| Home/News | Downloads | Forums | D32 Magazine | Resources | Info and Facts |  
 
 Creating simple property editor for your custom components


September 9th

Oleg Gopaniouk
In my previous article Listing all TDataBase components & BDE aliases I told you about getting BDE-related information. That information is the most useful for creating your data-aware components. In this article I want to explain how to create property editors. The property editors makes your components more useful. The creating of property editors is not very difficult task. At first, I want to suggest you to look at some properties. For example, look at the DatabaseName property of TQuery or TTable components. You can choose the necessary value from the list or type it from keyboard. In this article you can find an information about how to create such lists of values in your component's properties. Firstly, you need to choose a valid parent for your property editor. It's very important step. There are several standard base property editors in Delphi, which can be parents of all others.

TPropertyEditor - UBasic property editor for all property editors
TIntegerProperty - Used for all integer values (Byte, Word, Integer, LongInt, 1..50, etc)
TCharProperty - Used for all char values (Char,'A'..'Z', etc)
TEnumProperty - Used for all enumerated properties
TFloatProperty - Used for all real types (Float, Single, Double, etc)
TStringProperty - Used for all string types (String, ShortString, String[10], AnsiString, etc)
TSetProperty - Used for all set types
TClassProperty - Used for all object types
TMethodProperty - Used for all method types
TComponentProperty - Used for all TComponent properties


Secondly, you have to override some methods of your property editor. All property editors are the descendants of the TPropertyEditor class and they have common virtual methods, which can be overridden. Look at the next example.
Listing A: MyPropertyEditor.pas



unit MyPropertyEditor; 

uses dbtables,DsgnIntf;Classes; 

interface 
  type 
    TDBNamePropEditor = class(TStringProperty) 
      public 
        procedure GetValues(Proc:TGetStrProc);override; 
        function GetAttributes:TPropertyAttributes;override; 
        function GetName:string;override; 
   end;  

procedure Register; 

implementation 

procedure TDBNamePropEditor.GetValues(Proc:TGetStrProc); 
var 
  List:TStringList; 
  i:Integer; 
begin 
  Session.Active:=True; 
  List:=TStringList.Create; 
  try 
  Session.GetDatabaseNames(List); 
  for i :=0 to List.Count-1 do Proc(List.Strings[i]); 
  except 
    List.Free; 
  end; 
  List.Free; 
  Session.Active:=False; 
end; 

function TDBNamePropEditor.GetAttributes:TPropertyAttributes; 
begin 
  Result:=[paValueList,paSortList]; 
end; 

function TDBNamePropEditor.GetName:string; 
begin 
  Result:='DataBaseName'; 
end; 

procedure Register; 
begin
  RegisterPropertyEditor(TypeInfo(String),TMyComponent,'DataBaseName',TDBNamePropEditor);
end;
This example illustrates the simple property editor for the DatabaseName property. This property is like the same property in TQuery and TTable components. The interface part of unit contains declarations of the TDBNamePropEditor class, which is descendant of the TStringProperty class. The TStringProperty class is used as a parent in all property editors for the string properties. There are three methods in the TDBNamePropEditor class. You must override these methods. They are virtual methods which are inherited from the TPropertyEditor class. This set of methods for your property editor is minimal. However, the TStringProperty class has some other methods which can be overridden. Lets look at the implementation of these methods. The GetName method is very simple. It returns the name of property, which is indicated in Object Inspector. You, probably, do not have to override this method very often. The GetAttributes method returns an object, which type is the TPropertyAttributes class. The TPropertyAttributes class is declared as a set of values. These values determine characteristics of your property editor. The means of these values are in the following list.

paValueList - such means as a property editor can returns enumerated list of values for this property.
paSubProperties - such means as a property editor has subproperties.
paDialog - such means as property editor activates edit dialog box when method Edit is called.
paMultiSelect - such means as property editor may be shown when several components are selected.
paAutoUpdate - such means you must call SetValue method for each change in property editor.
paSortList - indicates so that values of list are sorted.
paReadOnly - indicates so that property is read only.
paRevertable - indicates so that property can be revert to start value.


The paValueList flag causes an appearance of combobox and the paDialog flag causes an appearance of button with dots. These flags can't be used together. In our example a value of the DatabaseName property can be chosen from the sorted list of strings. The GetValues method returns list of values, which appears in Object Inspector. It has one parameter of the TGetStrProc type. The TGetStrProc class is declared in the Classes.pas unit. You must call the TGetStrProc method with string parameter for putting this string value to the output list of values.

For example, you need to add 'Delphi' string to the list of values which appears in Object Inspector. In this case you have to write such code: Proc('Delphi'); The Register procedure is used by Delphi for components registering or registering of property editors. It's body contains the call of RegisterPropertyEditor procedure. The first parameter of the RegisterPropertyEditor procedure is the pointer on RTTI(Run-Time Type Information). This parameter is created by using the TypeInfo operator. For example, you have the TMyPropertyType property. You should use following code: TypeInfo(TMyPropertyType). Second parameter is the component type to which property editor apply. If this parameter is nil, then the property editor applies to all properties of first parameter type. Next parameter is the string which defines the name of property. If this value is an empty string, then the property editor applies to all properties which have the first parameter type. You can use second or third parameters for a restriction of the property editor domain. Last parameter is a class of the property editor which is registered. In conclusion I want to note that this article can be useful for creating all types of components.



 
 Hits/month  2,500,000+ 
 Downloads
 (Since May 2000)
 7,393,709 
 Total Files  6,023 
 Forum msgs  7,670 
 Articles/FAQs  70+/900+ 
Kylix
Tips n Tricks
FAQs
Knowledge Base
Bug Listings
Articles
Books
Newsgroups
Links
Submissions
Testimonials
Advertising
Contact Us
About Us
Search Amazon:
Top Selling Software at Amazon

| Home/News | Downloads | Forums | Resources | Info and Facts | Testimonials |
  Site Search:
 


Comments/Problems: Webmaster@delphi32.com
Copyright © 1998-2006, Delphi32.com. All rights reserved.
Terms of Use