Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loading an image when a click is done.

Status
Not open for further replies.

PaidtheUmpire

Programmer
Jan 4, 2004
105
AU
What I want to do is this....


I have a DBGrid which has 6 columns in it 5 displayed:
S, R, TeamA, Result, TeamB & FileLocation (hidden).


What I want to do is as follows...

When a user clicks on a row in the DBGrid say the following row:
5, 6, "Greenwood", "Lost To", "Sydney", "S5R6GS.txt" either one of the following should occur:

1) The file is not found, an image "FileNotThere" is shown in the image location "image2".
2) The file is found and an image "FileFound" is shown in the image location "image2".

How can i get this image thing working?
 
There are a couple of places where you could do this.

1. The OnCellClick event handler of the DBGrid. This means the user MUST click somewhere in the row in order for the information you want to appear. Scrolling using arrow keys, page up, page down, etc will not cause the display to be updated.

2. The OnDataChange event handler of the TDataSource. The problem with this is that if the user is scrolling rapidly through the grid, it will slow things down considerably.

3. If you're connected to the data using a TTable or a TADOTable, you can create a calculated field for the table and use the OnCalcFields event handler to set the value.

No matter how you handle it, you're probably going to have to use a calculated field, which means that option 3 is going to be your best bet. Your code will look something like this (assuming the calculated field is named 'FileOK'):
Code:
procedure TMyForm.MyTableCalcFields(ds: TDataSet);
begin
  if FileExists(MyTable.FieldByName('FileLocation')) then
    MyTable.FieldByName('FileOK').AsString := 'File Found'
  else
    MyTable.FieldByName('FileOK').AsString := 'File Not There';
end;

If the file location does not have a path specified, you'll want to include that in the call to FileExists.

-Dell
 
Thanks Hilfy,

but i think you missed what i was getting at...

I want to get the code which changes the image at "image2" location.
 
Ok...I think I get it now....

You could use the first technique I listed - the OnCellClick of the DBGrid. When a user clicks in the grid, check to see if they're on the same record as the last time they clicked - you can setup a variable to carry this information in.

If the record is different and the file exists, load it into the TImage - TImage1.Picture.LoadFromFile(filename);

Otherwise load your default FileNotThere image.

-Dell
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top