Mega Code Archive

 
Categories / Delphi / ADO Database
 

How to create a DataBaseName Property

Title: How to create a DataBaseName Property ? Question: The following example shows how to create a component with a DataBaseName property like the DataBaseName property of TQuery. The purpose of this property is to list all the available databases so that the user of the component can just select the required database. Answer: Please note that for this example I have put all the code in one unit, but if you want to make such a component you must make sure that you place all the design time code in a seperate unit. How is this done: - Remove the DesignIntf, DesignEditors units from the uses clause. - Create a new unit (xxxxDesignTime). - Move the Register procedure in to the new unit too. - All the none design code can stay in the other unit. - Make a package. - Add both units to the package. - Install the package. The reason for this is that it is and has been illegal to distribute the designtime code. Borland are now enforcing it by making key units only available in the IDE via packages. unit TestDataBaseNameProperty; interface uses Windows, Messages, SysUtils, Classes, DBTables, DesignIntf, DesignEditors; type TDBStringProperty = class(TStringProperty) public function GetAttributes: TPropertyAttributes; override; procedure GetValueList(List: TStrings); virtual; abstract; procedure GetValues(Proc: TGetStrProc); override; end; TDatabaseNameProperty = class(TDBStringProperty) public procedure GetValueList(List: TStrings); override; end; TTestDataBaseNameProperty = class(TComponent) private { Private declarations } FDataBaseName : String; //Stores the name of the selected database. protected { Protected declarations } public { Public declarations } constructor Create(AOwner: TComponent); override; destructor Destroy; override; published { Published declarations } property DataBaseName: String read FDataBaseName write FDataBaseName; end; procedure Register; implementation procedure Register; begin //Registry the property editor for the database names. RegisterPropertyEditor(TypeInfo(String),TLanguageChanger, 'DataBaseName',TDatabaseNameProperty); RegisterComponents('Self-made Components', [TLanguageChanger]); end; { TTestDataBaseNameProperty } constructor TTestDataBaseNameProperty.Create(AOwner: TComponent); begin inherited; end; destructor TLanguageChanger.Destroy; begin inherited Destroy; end; procedure TTestDataBaseNameProperty.setFormID(Value: Integer); begin FFormID := Value; end; procedure TTestDataBaseNameProperty.setLanguageID(value: Integer); begin FLanguageID := Value; end; { TDBStringProperty } function TDBStringProperty.GetAttributes: TPropertyAttributes; begin Result := [paValueList, paSortList, paMultiSelect]; end; procedure TDBStringProperty.GetValues(Proc: TGetStrProc); var I: Integer; Values: TStringList; begin Values := TStringList.Create; try GetValueList(Values); for I := 0 to Values.Count - 1 do Proc(Values[I]); finally Values.Free; end; end; { TDatabaseNameProperty } procedure TDatabaseNameProperty.GetValueList(List: TStrings); begin Session.GetDatabaseNames(List); end; end. Tested with Delphi 6 Professional on Windows 2000. Be sure to include ALL the UNITS. (Thanks to hvassbotn for the help).