C++Builder - Database tutorial, part 4At last, we shall load / save our articles database from / to a text file. Start by downloading the source code Articles4.zip and next unzip it to folder \CppProjects\Articles4. Saving to a fileWe added a variable ArticlesFile that shall contain the name of a text file. Of course, the file specified in ArticlesFile will not be found when you run your program for the first time. The function SaveArticles() is called right before our program terminates (see the FormDestroy handler).
void TForm1::SaveArticles() { int index; String S; TStringList *SL; SL = new TStringList; for (index = 0; index < Articles->Count; index++) { TArticle *Art = static_cast Loading from a fileNow, let's have a look at LoadArticles().
Cleaner code for updating the user interfaceTo UpdateUI, we added DBModeNew as a "default parameter", as can be seen in Unit1.h: In Unit1.cpp, we see: void TForm1::UpdateUI(String Stat, String DBModeNew) { stStatus->Caption = Stat; DBMode = DBModeNew; // more statements... } This simplifies the code for calling UpdateUI: if only one parameter is specified, DBModeNew is set
to the default value "B" (for "Browsing"). Example in btnCancelClick: void TForm1::UpdateGrid(int RowNo) { int index; Grid->RowCount = Articles->Count + 1; for (index = 0; index < Articles->Count; index++) { TArticle *Art = static_cast<tarticle*>(Articles->Objects[index]); Grid->Cells[0][index + 1] = Articles->Strings[index]; Grid->Cells[1][index + 1] = Art->Name; Grid->Cells[2][index + 1] = FormatFloat("0.00", Art->Price); Grid->Cells[3][index + 1] = IntToStr(Art->Stock); } if (RowNo > (Grid->RowCount - 1)) RowNo = 0; Grid->Row = RowNo; } |
|