Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load ImageList picture in TImage with a ComboBox

Tags:

delphi

In my Delphi form I have an ImageList with 4 pictures. There are also a ComboBox called ComboBox1 and a TImage component that is called Image9.

I've created an onChange for my ComboBox because I'd like to do a thing like this: if the ComboBox item 1 is selected, then load the image 1 in my ImageList. Same case if ComboBox item 3 is selected (for example), load the image 3 of ImageList.

The code I wrote is this:

case ComboBox1.Items[ComboBox1.ItemIndex] of
0:
 begin
  ImageList1.GetBitmap(0,Image9.Picture);
 end;
1:
 begin
  ImageList1.GetBitmap(1,Image9.Picture);
 end;
2:
 begin
  ImageList1.GetBitmap(2,Image9.Picture);
 end;
3:
 begin
  ImageList1.GetBitmap(3,Image9.Picture);
 end;
end;

With this code, the IDE (I'm using Delphi XE4) is giving me an error on case ComboBox1.Items[ComboBox1.ItemIndex] of because it says that an Ordinal type is required. What could I do?

like image 656
Alberto Miola Avatar asked Jan 12 '23 20:01

Alberto Miola


1 Answers

case statements in Delphi work on Ordinal types:

Ordinal types include integer, character, Boolean, enumerated, and subrange types. An ordinal type defines an ordered set of values in which each value except the first has a unique predecessor and each value except the last has a unique successor. Further, each value has an ordinality, which determines the ordering of the type. In most cases, if a value has ordinality n, its predecessor has ordinality n-1 and its successor has ordinality n+1

ComboBox.Items are strings, and therefore don't meet the requirements of being ordinals.

Also, as noted in your comment below, you can't assign to Image9.Picture directly as you are; you have to use Image9.Picture.Bitmap instead. In order for the TImage to update properly to reflect the change, you need to call it's Invalidate method.)

Change your case to use the ItemIndex directly instead:

case ComboBox1.ItemIndex of
  0: ImageList1.GetBitmap(0,Image9.Picture.Bitmap);
  1: ImageList1.GetBitmap(1,Image9.Picture.Bitmap);
end;
Image9.Invalidate;  // Refresh image

Or just go directly to the ImageList

if ComboBox1.ItemIndex <> -1 then
begin
  ImageList1.GetBitmap(ComboBox1.ItemIndex, Image9.Picture.Bitmap);
  Image9.Invalidate;
end;
like image 135
Ken White Avatar answered Jan 22 '23 10:01

Ken White