Data-Browse Data-Aware Control Making

zhaozj2021-02-11  198

Data-Browse Data-Aware Control Making

In the MIS system, the most common data perceived control is used. Learn how to write their own components, a very important principle is from the components you are familiar with, deriving a new control. In the data perceived control, the Data-Browse type is very simple and practical. So below we explain a custom TSundbText writing: TDBTEXT:

TsundbText = Class (TCUSTOMLABEL) {...} END;

Because DBText only plays a function of displaying data, it is a label with data-aware function, and derives from the Custom Series control according to the habit of Delphi, so we chose TCUSToMlabel.

How to have data aware functions, in fact, most of the books are not very clear, including Delphi Help. How many more, we slowly narrative: 1: Select a Datalink object that can be data-sensing, usually use TFieldDataLink, indicating that only one database field is associated with two: Components must provide two design when DataSource and DataField can be read Property 3: Components must process fdatalink's OndataChange events to reflect changes in data fields: The Notification Reserved method must be provided so that when the associated TDataSource component can be notified, and reflect this Changing five: habits, we have to handle the CM_GETDATALINK message to comply with the requirements of the VCL internal communication. Six: For the flexibility of the component, it is recommended to provide a field property to enhance the applicability of the programming.

Here we have to explain one by one: private FDataLink: TFieldDataLink; constructor Create (AOwner: TComponent); override; begin inherited; FDataLink: = TFieldDataLink.Create; {create a TFieldDataLink object} FDataLink.OnDataChange: = DataChange; {the TFieldDataLink The ONDATAANGE event processing of the object is handed over to TsundbText's DataChange to handle this to perform changes in the database field, and perform itself to process} END;

DESTRUCTOR Destroy; Begin {This is very simple, just recycled resources} fdataLink.free; fdataLink: = nil; inherited;

From above we have seen, the key to data perception is that the DataChange event, which we can know that it is necessary to give the display value of this field to the Caption property of the tag component, so the following code is to do this. of. (Note, the reason why you have to complete the assignment in multi-faceted considerations, including design period and running period, exceptions) procedure tsundbtext.datachange (Sender: Tobject); begin caption: = getfieldtext; {Read; {Read; {Read; Only Displaytext When Make An Data Browing Component} END;

Function Tsundbtext.getfieldText: String; Begin {Normal situation, just get the Field's Displaytext represented by FDATALINK, if you develop is not a Data-Browse control, please use other field properties} IF (fdataLink <> nil) THEN Result: = fdataLink.field.displaytext else if (csdesigning in componentstate) Then Result: = name else result: = '; {If it is in the design period, the label is displayed is the name of the component, if it is a running period, The label is empty; of course, this is just a habit, but for people using Delphi IDE, it is almost rules. } END; if there is no two properties of DataSource and DataField, then the control is almost impossible. This is also an important step.

Property DataSource: TDataSource ReadDataSource; Property DataField: String Read GetDataField Write setDatafield; these methods are very simple, just access TfieldDataLink DataSource and FieldName properties. Function TsundbText.getDataSource: TDataSource; begin result: = fdataLink.dataSource; {Simply returned TFieldDataLink DataSource Property From this, the method of writing the DataSource property is also assigning FDATALINK.} END;

Procedure tsundbtext.setDataSource (value: tdataroup); begin fdataLink.DataSource: = value; {as cloud, assignment to DataSource attribute}

IF (value <> nil); {This is important, Tcomponent provides a FreeNotification method, so that when Value is released, the Acomponent's Notification method is automatically called, we will The corresponding Data-Browse control can be adjusted to reflect this change. If the DataSource component corresponding to TsundbText is removed, it should be reflected on the label.

Function tsundbtext.getdatafield: string; begin result: = fdataLink.fieldName; {get the tfieldDatalink FieldName property, and know which database field name is connected;

Procedure tsundbtext.SetDatafield (const value: string); begin fdataLink.fieldName: = value; {This should not explain} END;

Only, we said the origin of the Notification, under the following: Procedure TsundbText.Notification (Acomponent: Tcomponent; Operation: TOPERATION); begin inherited; if (Operation = OpRemove) and (fdataLink <> nil) and ACOMPONENT = DATASOURCE) THEN DATASOURCE: = NIL; END; When the association component is deleted, and the deleted DataSource component is the DataSource component associated with it, it should set its DataSource property empty.

Function Tsundbtext.getfield: Tfield; Begin Result: = fdataLink.field;

Finally, the data perceived control also responds to the CM_GetDataLink message. Typically, the processing is to return TDATALINK's RESULT field (VAR message: tMgetDataLink (Var Message: TMGETDATALINK); Message Cm_GetDataLink;

Procedure cmgetdataLink (var message: tMgetage); begin message.result: = integer (fdataLink); end; here, the functionality of the basic components is already complete, however, there are many component writers who have not noticed, ie action Applications. If the custom component can integrate into the VCL's Action mechanism, you will inevitably make the custom control more powerful and more advanced users like.

Next time, let's talk about Action's dragon debro.

转载请注明原文地址:https://www.9cbs.com/read-4965.html

New Post(0)