简体   繁体   中英

Loading picture from file with filename as a changing variable

I'm currently busy with a project, extremely tired and I can't think straight, and I'm having some trouble loading a picture from a file, where the name of the picture is stored in a variable that changes according to what someone selects in a combobox. The combobox is sorted and the first ItemIndex is the name of the first piece in the table.

Here's the (horrifying and mediocre) code for declaring the file name to a variable:

begin
  iTel := 0;

  with dmMusiekstukke do
  begin
    while not (tblMusiekstukke.Eof) AND (iTel <= 4) do
    begin
      tblMusiekstukke.First;
          Inc(iTel) ;
       arrMusic[itel] := tblMusiekstukke['Musicpiece name'] ;
       tblMusiekstukke.Next ;
      end;
  end;

        case cbbMusic.ItemIndex of
        0 : Image := arrMusic[1] ;
        1 : image := arrMusic[2] ;
        2 : Image := arrMusic[3] ;
        3 : Image := arrmusic[4] ;

        end;

       end;

The file name of the image is exactly the same as the name of the item in the databases's 'Musicpiece name' field. The array (of string) should be filled with the name of the Musicpiece, which in turn is also the name of the file. Out of the combobox, the person selects the music piece they want and the picture for that specific music piece is displayed.

Code I have for loading the file :

procedure TfrmSheets.FormShow(Sender: TObject);
begin

imgSheet.Picture.LoadFromFile(  '''' +  frmMusic.Image + '.png');
end;

end.

As you can see I'm really not sure how to do it. All the pictures are in .png format. Thank you very much.

be careful. if frmMusic.Image contains only the file name, then the picture file must reside in the \\you_project\\Win32\\Debug

Can you check, what contains frmMusic.Image

Try put this value to Label

Alright I sorted it out. It worked by not adding apostrophes on either sides, that was one of my problems, but the other solution to a problem I found here: The system cannot find the file specified

I then did this:

imgSheet.Picture.LoadFromFile(frmMusic.Image + '.png.png');

I also removed

tblMusiekstukke.First ; 

out of the While loop, as it was constantly placing the marker of the table to first item. It now works perfectly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM