C++ Builder Tutorials

C++Builder - Database tutorial, part 3

In this part of our tutorial, we add functions for editing and deleting records.
We also rearrange and clean up the source code, trying to avoid code repetition as much as possible.

Start by downloading the source code Articles3.zip and next unzip it to folder \CppProjects\Articles3.


Cleaning up the code

We added the local variable DBMode, that we set to "B" for browse mode, "A" for adding articles and "E" for editing.

A new function UpdateUI() groups the user inteface actions. It shows status messages, and depending on the value of DBMode, it shows or hides the panel with the edit boxes, enables/disables buttons, and so on:

void TForm1::UpdateUI(String Stat)
{
  stStatus->Caption = Stat;
  Panel1->Visible = ((DBMode == "A") || (DBMode == "E"));
  // disable buttons when Panel1 is visible
  btnAdd->Enabled = !(Panel1->Visible);
  btnEdit->Enabled = !(Panel1->Visible);
  btnDelete->Enabled = !(Panel1->Visible);
  btnFindID->Enabled = !(Panel1->Visible);
  btnFindName->Enabled = !(Panel1->Visible);
  
  if (DBMode == "A") { 
    // clear edit boxes
    edName->Text = "";
    edPrice->Text = "";
    edStock->Text = "";
  }
  if ((DBMode == "A") || (DBMode == "E")) 
    edID->SetFocus(); // place cursor
}

UpdateUI() is called by several onclick event handlers of the buttons, thus simplifying their code. Look for example how it is used here:

void __fastcall TForm1::btnAddClick(TObject *Sender)
{
  DBMode = "A";
  UpdateUI(" Adding article ");
}

Editing and deleting

We added a button for editing records:

void __fastcall TForm1::btnEditClick(TObject *Sender)
{
  int index;
  if (Grid->Row < 1)
    stStatus->Caption = " Select article to be edited ";
  else {
    DBMode = "E";
    UpdateUI(" Editing article ");
    index = Grid->Row - 1;
    TArticle *Art = static_cast<TArticle*>(Articles->Objects[index]);
    edID->Text = Articles->Strings[index];
    edName->Text = Art->Name;
    edPrice->Text = FormatFloat("0.00", Art->Price);
    edStock->Text = IntToStr(Art->Stock);
  }
}

Here's what the button for deleting records does:

void __fastcall TForm1::btnDeleteClick(TObject *Sender)
{
  int index;
  if (Grid->Row < 1)
    stStatus->Caption = " Select article to be deleted ";
  else {
    if (MessageDlg("Really delete this article?",
                   mtConfirmation, mbYesNoCancel, 0) == mrYes) {
      Articles->Delete(index);
      UpdateGrid();
      stStatus->Caption = " Article was deleted ";
    }
    else
      stStatus->Caption = " Article was not deleted ";
  }
}

be polite :)We don't simply delete a record without asking for confirmation.
Instead, with MessageDlg() we show a message dialog, that returns a value depending on the button that was clicked by the user:

  • it shows the message "Really delete this article?"
  • the message-type is mtConfirmation
  • it has 3 buttons: Yes, No and Cancel
  • it returns the value mrYes if the Yes button is clicked, or another value for the others

Table of contents

  1. Text Database
  2. DB Browser
  3. Edit and Delete
  4. Load / Save
  5. Validating