Board index » cppbuilder » dynamically assign a name to a TImage

dynamically assign a name to a TImage


2006-09-19 10:56:36 PM
cppbuilder56
How can I do the following?
I have 10 TImages on a Form named Img1, Img2,...Img10. In my database I have a table with three columns: ID, Image and Name:
ID Image Name
1 Img1 Name1
2 Img2 Name2
...
10 Img10 Name10
I do a select stament and I collect the data from that table. what I want to do is to dynamically assing the Image name of Img1...Img10 to the one I get from my query. So something like:
While (!qry->Eof)
{
//assign the name here
...->Name = qry->FieldByName("Name")->AsString;
qry->Next();
}
Thanks in advance
 
 

Re:dynamically assign a name to a TImage

"Danid" < XXXX@XXXXX.COM >wrote in message
Quote
//assign the name here
...->Name = qry->FieldByName("Name")->AsString;
You already know what you need to do, so what exactly are you having a
problem with?
Gambit
 

Re:dynamically assign a name to a TImage

The problem I am having is what to put in the ... ?
Should I declare a TImage variable and do some cast?
Your help would be very appreciated.
"Remy Lebeau \(TeamB\)" < XXXX@XXXXX.COM >wrote:
Quote

"Danid" < XXXX@XXXXX.COM >wrote in message
news:45100524$ XXXX@XXXXX.COM ...

>//assign the name here
>...->Name = qry->FieldByName("Name")->AsString;

You already know what you need to do, so what exactly are you having a
problem with?


Gambit


 

{smallsort}

Re:dynamically assign a name to a TImage

"Danid" < XXXX@XXXXX.COM >wrote in message
Quote
The problem I am having is what to put in the ... ?
How are you creating your TImage objects to begin with?
Gambit
 

Re:dynamically assign a name to a TImage

My TImages are created by droping a TImage control on a From.
"Remy Lebeau \(TeamB\)" < XXXX@XXXXX.COM >wrote:
Quote

"Danid" < XXXX@XXXXX.COM >wrote in message
news:4510360b$ XXXX@XXXXX.COM ...

>The problem I am having is what to put in the ... ?

How are you creating your TImage objects to begin with?


Gambit


 

Re:dynamically assign a name to a TImage

"Danid" < XXXX@XXXXX.COM >wrote in message
Quote
My TImages are created by droping a TImage control on a From.
I suggest you put them into an array when the form is created, and then your
loop can easily index them, regardless of what their names actually are.
For example:
private:
TImage *Images[10];
__fastcall TForm1::TForm1(TComponent *Owner)
: TForm(Owner)
{
Images[0] = Image1;
Images[1] = Image2;
Images[2] = Image3;
Images[3] = Image4;
Images[4] = Image5;
Images[5] = Image6;
Images[6] = Image7;
Images[7] = Image8;
Images[8] = Image9;
Images[9] = Image10;
}
{
for(int i = 0; (i < 10) && (!qry->Eof); ++i)
{
Images[i]->Name = qry->FieldByName("Name")->AsString;
qry->Next();
}
}
Gambit
 

Re:dynamically assign a name to a TImage

"Remy Lebeau \(TeamB\)" < XXXX@XXXXX.COM >wrote:
Quote

"Danid" < XXXX@XXXXX.COM >wrote in message
news:45103c25$ XXXX@XXXXX.COM ...

>My TImages are created by droping a TImage control on a From.

I suggest you put them into an array when the form is created, and then your
loop can easily index them, regardless of what their names actually are.
For example:

private:
TImage *Images[10];

__fastcall TForm1::TForm1(TComponent *Owner)
: TForm(Owner)
{
Images[0] = Image1;
Images[1] = Image2;
Images[2] = Image3;
Images[3] = Image4;
Images[4] = Image5;
Images[5] = Image6;
Images[6] = Image7;
Images[7] = Image8;
Images[8] = Image9;
Images[9] = Image10;
}

{
for(int i = 0; (i < 10) && (!qry->Eof); ++i)
{
Images[i]->Name = qry->FieldByName("Name")->AsString;
qry->Next();
}
}


Gambit


Many thanks for your help,
I didn't try the solution you suggested, but I do't think it is going to work for my case.
Here is an explanation what I want to do:
On my from I want to put 50 empty (no picture) small TImages distributed according to a specific design
(5 cols, 10 rows). Each TImage has a fixed X, Y position; a fixed Height and Size and
a name: Img1 Img2 ... Img50;
Each Image has an OnClick event, here is an example:
Img1OnClick(TObject *Sender)
{
CallMethod(Sender);
}
public void CallMethod(TObject *Sender)
{
TImage *Image = dynamic_cast<TImage*>(Sender);
String name = Image->Name;
CallAnotherMethod(name);
}
In the table in my database (tblImage: ID, Image, Name)
ID Image Name
1 Img1 name1
2 Img2 name2
...
35 Img35 name35
36 Img36 null
...
49 Img49 null
50 Img50 null
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.
If I start my application, I want that the data from that table is read, which is not a problem (Query), and then do the following:
- loop through the Query using a while statement.
- get the names of the Images from the query: String ImgName = Qry->FieldByName("Name")->AsString;
- Load the pictures of the Images(Img1-Img35): ###->Picture->LoadFromFile("/PICS/" + ImgName + ".jpg"). This is only applicable for
the pictures where the name is not empty or null.
- change the name of the Img1-Img35 with the name from the table: ###->Name = ImgName. This name is used later to pass it to the method CallAnotherMethod(ImgName) when the user clicks on a picture.
So what to put in the ### ?
Thanks,
 

Re:dynamically assign a name to a TImage

"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