"Danid" <
XXXX@XXXXX.COM >wrote in message
Quote
I didn't try the solution you suggested, but I do't think it is going to
work for my case.
Yes, it will. Keep reading.
Quote
On my from I want to put 50 empty (no picture) small TImages
distributed according to a specific design (5 cols, 10 rows).
I would not recommend that approach. That uses a lot of unnecessary
resources. You should consider using a TDrawGrid instead.
Quote
The Name field is not mandatory, it could be empty. I want to give to
a user the ability to maintain this table so he can change the names of
the images.
Then you should be storing the names separately from the TImage objects. Do
not use the Name property, especially since you have database fields that
can have empty names. A component cannot have an empty name. Using my
earlier example, simply expand the array to store additional values, ie:
struct sImageInfo
{
TImage *Image;
AnsiString Name;
};
private:
sImageInfo ImageInfo[10][5];
__fastcall TForm1::TForm1(TComponent *Owner)
: TForm(Owner)
{
ImageInfo[0][0].Image = Image1;
Image1->Tag = 0;
//...
ImageInfo[9][4].Image = Image50;
Image50->Tag = 49;
}
void __fastcall TForm1::LoadImages()
{
for(int i = 0; i < 10; ++i)
{
for(int j = 0; j < 5; ++j)
{
ImageInfo[i][j].Image->Picture = NULL;
ImageInfo[i][j].Name = "";
}
}
// perform database query here ...
for(int i = 0; (i < 10) && (!qry->Eof); ++i)
{
for(int j = 0; (j < 5) && (!qry->Eof); ++j)
{
ImageInfo[i][j].Name = qry->FieldByName("Name")->AsString;
if( ImageInfo[i].Name != "" )
ImageInfo[i].Image->Picture->LoadFromFile("c:\\some
folder\\PICS\\" + qry->FieldByName("Image") + ".jpg");
qry->Next();
}
}
}
void __fastcall TForm1::ImageClick(TObject *Sender)
{
TImage *pImage = static_cast<TImage*>(Sender);
int row = pImage->Tag / 5;
int col = pImage->Tag % 5;
CallAnotherMethod(ImageInfo[row][col].Name);
}
If you go the TDrawGrid approach, then the code becomes the following
instead:
#include <jpeg.hpp>
struct sImageInfo
{
TJPEGImage *Image;
AnsiString Name;
};
private:
sImageInfo ImageInfo[10][5];
__fastcall TForm1::TForm1(TComponent *Owner)
: TForm(Owner)
{
ImageInfo[0][0].Image = NULL;
//...
ImageInfo[9][4].Image = NULL;
}
void __fastcall TForm1::LoadImages()
{
for(int i = 0; i < 10; ++i)
{
for(int j = 0; j < 5; ++j)
{
delete ImageInfo[i][j].Image;
ImageInfo[i][j].Image = NULL;
ImageInfo[i][j].Name = "";
}
}
// perform database query here ...
for(int i = 0; (i < 10) && (!qry->Eof); ++i)
{
for(int j = 0; (j < 5) && (!qry->Eof); ++j)
{
ImageInfo[i][j].Name = qry->FieldByName("Name")->AsString;
if( ImageInfo[i].Name != "" )
{
ImageInfo[i].Image = new TJPEGImage;
ImageInfo[i].Image->LoadFromFile("c:\\some
folder\\PICS\\" + qry->FieldByName("Image") + ".jpg");
}
qry->Next();
}
}
}
void __fastcall TForm1::DrawGrid1Click(TObject *Sender)
{
int row = DrawGrid1->Row;
int col = DrawGrid1->Col;
if( (row>= 0) && (col>= 0) )
CallAnotherMethod(ImageInfo[row][col].Name);
}
void __fastcall TForm1::DrawGrid1DrawCell(TObject* Sender, int ACol, int
ARow, TRect Rect, TGridDrawState State)
{
if( State.Contains(gdSelected) )
DrawGrid1->Canvas->Brush->Color = clHighlight;
else
DrawGrid1->Canvas->Brush->Color = DrawGrid1->Color;
DrawGrid1->Canvas->FillRect(Rect);
if( ImageInfo[ARow][ACol].Image )
DrawGrid1->Canvas->Draw(Rect.Left, Rect.Top,
ImageInfo[ARow][ACol].Image);
if( State.Contains(gdFocused) )
DrawGrid1->Canvas->DrawFocusRect(Rect);
}
Quote
So what to put in the ### ?
You already knew the answer to that from my earlier reply, because I showed
you exactly how to access the TImage objects.
Gambit