Write Code to Handle Minor Events (dbExpress Tutorial)
Go Up to Tutorial: Using dbExpress to View and Update Databases in an Application
Now we create handlers for the remaining events.
Contents
Open Check Box Click
With the TCheckBox component selected, double-click the OnClick
event on the Event tab in the Object Inspector. Add the following code:
Delphi
procedure TForm2.CheckBoxActiveClick(Sender: TObject);
begin
// If checked, activate the client data set; if unchecked, deactivate it.
ClientDataSet1.Active := CheckBoxActive.Checked
end;
C++
void __fastcall TForm1::CheckBoxActiveClick(TObject *Sender)
{
// If checked, activate the client data set; if unchecked, deactivate it.
ClientDataSet1->Active = CheckBoxActive->Checked;
}
Activating the TClientDataSet causes the SQL command in the TEdit labeled by SQL: to be executed. See the Change in SQL Command section below for more information.
TClientDataSet to Close
This event is triggered just before the TClientDataSet closes, which happens when another table is selected.
Clicking a table cell displays data from a table cell in a form, MemoForm
, which is a TFormCurrentField
object. Define TFormCurrentField
in Add Cell Viewing Form. This event handler destroys the TFormCurrentField
.
Generate the event handler stub as usual by double-clicking the BeforeClose
event for the TClientDataSet component.
Delphi
procedure TForm2.ClientDataSet1BeforeClose(DataSet: TDataSet);
begin
// If a form to display the table cell contents exists, destroy it.
if Assigned(MemoForm) then
begin
MemoForm.DBMemoCurrentField.DataField := '';
if MemoForm.Visible then
MemoForm.Close;
end;
end;
C++
void __fastcall TForm1::ClientDataSet1BeforeClose(TDataSet *DataSet)
{
// If a form to display the table cell contents exists, destroy it.
if (MemoForm)
{
MemoForm->DBMemoCurrentField->DataField = "";
if (MemoForm->Visible)
MemoForm->Close();
}
}
Apply Updates Button Click
Clicking the Apply Updates TButton applies database updates, that is, writes the changed records to the database. Although this update is accomplished by calling TClientDataSet.ApplyUpdates, the TDataSetProvider actually performs the update for the data set. See the Add Database Components comments on TDataSetProvider.
Double-click the OnClick
event for the Apply Updates TButton and add this code to the generated code skeleton.
Delphi
procedure TForm2.ButtonApplyClick(Sender: TObject);
var
X, Y: Integer;
begin
// Client data set has provider do update.
ClientDataSet1.ApplyUpdates(-1);
Y := Self.Top + Self.ButtonApply.Top;
X := Self.Left + Self.ButtonApply.Left;
// Ask user if data should be refreshed in grid display.
if MessageDlgPos('Refresh?',mtCustom, [mbYes, mbNo], 0, X, Y) = mrYes then
begin
ClientDataSet1.Close;
ClientDataSet1.Open;
end;
end;
C++
void __fastcall TForm1::ButtonApplyClick(TObject *Sender)
{
int X, Y;
// Client data set has provider do update.
ClientDataSet1->ApplyUpdates(-1);
Y = this->Top + this->ButtonApply->Top;
X = this->Left + this->ButtonApply->Left;
// Ask user if data should be refreshed in grid display.
if (MessageDlgPos("Refresh?",mtCustom, mbYesNo, 0, X, Y) == mrYes)
{
ClientDataSet1->Close();
ClientDataSet1->Open();
}
}
Tip: Instead of using this button, you can use the ApplyUpdates and CancelUpdates of the Vcl.DBCtrls.TDBNavigator control.
Change in Filter TEdit
As text changes in the TEdit containing the filter, the list of tables in the TListBox also changes. PopulateListBox
is a utility that lists the currently filtered tables in the TListBox.
Double-click the OnChange
event for the filter TEdit to create the stub, then add the code below.
Delphi
procedure TForm2.EditFilterChange(Sender: TObject);
begin
if SQLConnection1.Connected then
PopulateListBox; //repopulate table list box, based on new filter text
end;
C++
void __fastcall TForm1::EditFilterChange(TObject *Sender)
{
if (SQLConnection1->Connected)
PopulateListBox(); //repopulate table list box, based on new filter text
}
Enter and Exit Filter TEdit
The OnEnter
and OnExit
events refer to the TEdit (containing the filter text) becoming active and becoming inactive, that is, getting and losing focus. Add TEdit event handlers as described previously, then add the following code:
Delphi
procedure TForm2.EditFilterEnter(Sender: TObject);
begin
// Blank text when the field is activated.
EditFilter.Text := '';
end;
procedure TForm2.EditFilterExit(Sender: TObject);
begin
// If blank and the field is deactivated, change text back to original hint.
if EditFilter.Text = '' then
EditFilter.Text := 'Filter (* as wild)';
end;
C++
void __fastcall TForm1::EditFilterEnter(TObject *Sender)
{
// Blank text when the field is activated.
EditFilter->Text = "";
}
void __fastcall TForm1::EditFilterExit(TObject *Sender)
{
// If blank and the field is deactivated, change text back to original hint.
if (EditFilter->Text == "")
EditFilter->Text = "Filter (* as wild)";
}
Change in SQL Command
When the text in the TEdit containing the SQL command changes, you want to update the SQL command text for the TSQLDataSet component. When you check the Open TCheckBox, the TClientDataSet activates, resulting in the new SQL command executing and updating the database data. As a result, you can change the SQL command, then check the TCheckBox to execute the command.
Double-click the OnChange
event for the SQL command TEdit and add this code.
Delphi
procedure TForm2.EditSQLChange(Sender: TObject);
begin
SQLDataSet1.CommandText := EditSQL.Text;
end;
C++
void __fastcall TForm1::EditSQLChange(TObject *Sender)
{
SQLDataSet1->CommandText = EditSQL->Text;
}
Form Destroy Event
When the application terminates, you want to perform some cleanup: closing the database connection and freeing objects that are no longer needed.
Select the form, then double-click the OnDestroy
event in the Object Inspector. Add the code for this event.
Delphi
procedure TForm2.FormDestroy(Sender: TObject);
begin
// Close the connection.
if SQLConnection1.Connected then
SQLConnection1.Close;
// Free objects.
if Assigned(FMetaDataProvider) then
FreeAndNil(FMetaDataProvider);
if Assigned(MemoForm) then
FreeAndNil(MemoForm);
if Assigned(AllTables) then
FreeAndNil(AllTables);
end;
C++
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
// Close the connection.
if (SQLConnection1->Connected)
SQLConnection1->Close();
// Free objects.
if (FMetaDataProvider)
{
delete FMetaDataProvider;
FMetaDataProvider = NULL;
}
if (MemoForm)
{
delete MemoForm;
MemoForm = NULL;
}
if (AllTables)
{
delete AllTables;
AllTables = NULL;
}
}
Previous
Develop Code to Handle Main Events