C++Builder - Database tutorial, part 3In this part of our tutorial, we add functions for editing and deleting records. Start by downloading the source code Articles3.zip and next unzip it to folder \CppProjects\Articles3. Cleaning up the codeWe added the local variable DBMode, that we set to "B" for browse mode, "A" for adding articles and
"E" for editing. 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 deletingWe 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 "; } }
|
|