1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
//---------------------------------------------------------------------------
// Saving and loading a StringGrid to and from file
// --------------------------------------------------
// (2000) DrDelphi
#include
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SaveGridToFile(TStringGrid *Grid,AnsiString ofilename)
{ TStringList *list= new TStringList; //create the list to save
for (int rowint=0;rowintRowCount;rowint++)
{
for (int colint=0;colintColCount ;colint++)
{
list->Add(Grid->Cells[rowint][colint]); //assign the cells string to list
}
}
list->SaveToFile(ofilename); //save the list to file
delete list; // free the list from memory
}
//---------------------------------------------------------------------------
void __fastcall TForm1::LoadGridFromFile(TStringGrid *Grid,AnsiString ofilename)
{
TStringList *list=new TStringList; //create the list to populate
list->LoadFromFile(ofilename); //load it from file
/// assign each line to its cell in the grid
for (int listint=0;listintCount;listint++)
{
for (int rowint=0;rowintRowCount;rowint++)
{
for (int colint=0;colintColCount ;colint++)
{
Grid->Cells[rowint][colint]=list->Strings[0];
list->Delete(0);
}
}
}
delete list; ///free the list
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender) //Event handler
{
//populate the Grid's cells with some data
for (int rowint=0;rowintRowCount;rowint++)
{
for (int colint=0;colintColCount ;colint++)
{
StringGrid1->Cells[rowint][colint]=IntToStr(rowint)+"<>"+IntToStr(colint);
}
}
// blank a few out to show the save maintains this relationship
StringGrid1->Cells[0][0]="";
StringGrid1->Cells[3][3]="";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender) //Event handler
{
//call the Save function
SaveGridToFile(StringGrid1,"C:\\windows\\desktop\\griddata.txt");
//call the Clear function
ClearGrid(StringGrid1);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender) //Event handler
{
//call the Load function
LoadGridFromFile(StringGrid1,"C:\\windows\\desktop\\griddata.txt");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ClearGrid(TStringGrid *Grid)
{
//iterate through the entire grid, clearing each cell
for (int rowint=0;rowintRowCount;rowint++)
{
for (int colint=0;colintColCount;colint++)
Grid->Cells[rowint][colint]="";
}
} |
Partager