How to write like TTable, Tadotable, TQuery, TadoQuery, etc. Auto drop-down display Field property Delphi VCL has many source code in Delphi, but some key attribute editors can't find it. Here I will give a few examples to explain. We can study two key properties of TDBEDIT a DataSource, DataField.
Example TtestControl = Class (TwinControl) private fdataLink: TfieldDataLink; // Defines the property of the property of the storage field Function getDatafield: string; function getDataSource: TDataSource;
procedure SetDataField (const Value: string); procedure SetDataSource (const Value: TDataSource); // property ResultField to use when published // described below: string read GetDataField write SetDataField; property DataField: string read GetDataField write SetDataField; property DataSource: TDataSource ReadDataSource Write setDataSource;
end; // get the field name function TTestcontrol.GetDataField: string; begin Result: = FDataLink.FieldName; // end; // get the data source function TTestcontrol.GetDataSource: TDataSource; begin Result: = FDataLink.DataSource; end; // set field name procedure TTestcontrol.SetDataField (const Value: string); begin if not (csDesigning in ComponentState) then ResetMaxLength; FDataLink.FieldName: = Value; end; // set the data source procedure TTestcontrol.SetDataSource (const Value: TDataSource); Begin if not (fdataLink.dataSourceFixed and (cslineing in componentstate)) The fdataLink.DataSource: = value; if value <> nil kilure.freenotification (self); end; When you implement such a control, as long as you place on the interface A TadoQuery, and turned TadoQuery, you specified DataSource after TTestControl, you will find that DataField has been pulled, do not write what property editor, this is because Delphi internal property editor is registered for all Delphi controls, All attribute specifications that match DataSource, DataField are automatic. If you change the names in the above controls, the part of the comment is broken, DataField is changed to Resultfield, you can't pull it automatically, you can only enter the string (actually there is still an attribute editor, at least the string), this Just write a property editor yourself and register to your own control.
I write as a property editor TListFieldProperty = class (TStringProperty) // inherited from TStringProperty, because a string FieldName public function GetAttributes: TPropertyAttributes; override; procedure GetValueList (List: TStrings); virtual; procedure GetValues (Proc: TGetStrProc) Override; function getDataSourcePropname: string; virtual; end; {tlistfieldproperty}
Function TLISTFIELDPROPERTY.GETATRIBUTES: TPROPERTYATTRIBUTES; Begin Result: = [PAVALUELIST, PASORTLIST, PAMULTISELECT]; END;
Function TLISTFIELDPROPERTY.GETDATASOURCEPROPNAME: STRING; Begin Result: = 'DATASOURCE'; / / Specify field drop-down list to what properties Value end;
Procedure TlistfieldProperty.getValuelSt (List: tstrings); var DataSource: TDataSource; begin // Take out all fields from the attribute specified above the above GetDataSourcePropname method Used to drop the display // Because the attributes defined in this control are TDataSource here, If it is TadoQuery, or other derived on // tdataSet class, just need to define a Dataset without the need for TDataSource, DataSource: = getPropertyValue (getComponent (0), getDataSourcePname) AS TDATASOURCE; if (Datasource <> nil) and (DataSource <> nil) DataSource.DataSet <> nil). DataSource.DataSet.GetfieldNames (List);
Procedure TLISTFIELDPROPERTY.GETVALUES (Proc: TgetstrProc); VAR i: integer; value: tstringlist; begin // Used to display list values: = tstringlist.create; try getValuelist (value); for i: = 0 To valuees.count - 1 Do Proc (Values [I]); FINALLY VALUES.FREE; END;
The attribute editor has been written, we need to register this property editor, register code as followsterPropertyEditor (TypeInfo (String), TListFieldProperty; you can also give you a registered property plus a class categoryName, we Specify the above ResultField property as a Database type property, registration examples are as follows
: RegisterPropertiesInsIncategory ('Database', TTestControl, ['Resultfield']);
Assuming that the above control TTestControl does not have a DataSource property, it cannot be successful, so which data source is specified in the property editor method, assumes that your field is the listSource property, which specifies function tListfieldProperty.GetDataSourcePropname: string; begin Result: = 'ListSource'; // Specify field drop-down list to what attribute to value END; I believe that there are many friends who have left it, realize the data-sensing controls, because the difficulties are here, other code in Delphi You can find the next time, I will talk about another difficult point, write about the collection of attribute editor, such as DBGRID TColumn style