How To: Making a control data-aware

Posted: (EET/GMT+2)

 

How To: Making a control data-aware

Level: Beginner, Intermediate


If you create applications which use databases (who doesn't?), you will without doubt use Delphi's data-aware controls like TDBEdit, TDBListBox or even TDBGrid. However, some controls just aren't data-aware. For example, I needed to have a data-aware version of the TTrackBar Win32 common control. Because one didn't exist, I needed to create it. Read on, and learn how to make your own TDBTrackBar.

The thing that makes a control data-aware is that it can respond to changes in the underlying database. For example, if you hook a TDBEdit with a dataset (a table on your database), and scroll to a different record, the text on the edit field changes automatically. Of course, updating the data on the control can be easily done manually, but it's much more convinient to have the control do this automatically.

The first step in making a control data-aware is defining something called a field link. A "field link" is an object which helps the control retrieve data from the dataset. A field link has two very important events, named OnDataChange and OnUpdateData.

OnDataChange event occurs whenever the control needs to update its visual appearance because the data on the dataset has changed. For example, this can occur if the user scrolls to another record.

OnUpdateData event occurs when the user has manipulated your control, and it's time to save the value represented by the control to the dataset. For example, this occurs when the user leaves your control.

Handling these two event is quite easy:

    FDataLink := TFieldDataLink.Create;
    FDataLink.OnDataChange := DataChange;
    FDataLink.OnUpdateData := UpdateData;
    
    ...
    
    Procedure TDBTrackBar.DataChange(Sender : TObject);
    Begin
      If (FDataLink.Field = nil) Then Position := Min
      Else Position := FDataLink.Field.AsInteger;
    End;
    
    Procedure TDBTrackBar.UpdateData(Sender : TObject);
    Begin
      FDataLink.Field.AsInteger := Position;
    End;
    


The next step is to make two new properties available. The properties we need to add are of course the same as every data-aware controls has: DataSource and DataField. Adding these properties is not different from adding any other properties to any component.

The last thing to do is to make sure the field value is modified every time the user manipulates the control. In the case of TTrackBar, we need to respond to two notification messages, CN_HSCROLL and CN_VSCROLL. When we get such messages, we do:

    Procedure TDBTrackBar.CNVScroll(Var Msg);
    Var Save : Integer;
    Begin
      Save := Position;
      If FDataLink.Edit Then Begin     { try to go into edit mode }
        FDataLink.Modified;
        Inherited;
      End
      Else Position := Save;
    End;
    


Here, we try to put the dataset into edit mode, if it already wasn't in one. If this fails, we simply put the position of the track bar back to the original.

Now you know at least something about creating a data-aware control. Of course, if you want to fully understand the code, open up your Component Writer's Guide (if you use Delphi 2), or the Developer's Guide (if you use Delphi 3). It's all there!


Code for this How To

  • Environment: Windows 95 or NT 4.0
  • Compiler: Delphi 2.0 or 3.0 for Win32
  • Memory: 4 MB RAM, 4k disk
  • Filename: dataawar.zip
  • Size: 1 762 bytes, 2kB
  • Date (M/D/Y): 08/02/97 01:00AM
  • Download time: 14.4k: about 1 second, 28.8k: quite fast
  • Distribution: Freeware
  • Registration price: none
  • Download!


Questions? Comments? Give feedback. Or just fill in a form.