Upload ImageList to TImage with ComboBox

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

I created onChange for my ComboBox because I would like to do something like this: if ComboBox element 1 is selected, load image 1 into your ImageList. In the same case, if ComboBox element 3 is selected (for example), load image 3 from ImageList.

The code I wrote is as follows:

 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; 

Using this code, the IDE (I use Delphi XE4) gives me an error on the case ComboBox1.Items[ComboBox1.ItemIndex] of , because it says that the type Ordinal is required. What can I do?

+4
source share
1 answer

case statements in Delphi Ordinal types :

Ordinal types include integer, character, boolean, enumerated, and sub-range types. The 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. In addition, each value has an ordinality that determines the type order. In most cases, if the ordinality n is of value, its predecessor is of the order of n-1, and its successor is of the order of n + 1

ComboBox.Items are strings and therefore do not meet the requirements of ordinals.

Also, as stated in the comment below, you cannot assign Image9.Picture directly, just like you; you should use Image9.Picture.Bitmap . To properly update TImage to reflect the change, you need to call the Invalidate method.)

Change case instead of ItemIndex 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 ImageList

 if ComboBox1.ItemIndex <> -1 then begin ImageList1.GetBitmap(ComboBox1.ItemIndex, Image9.Picture.Bitmap); Image9.Invalidate; end; 
+9
source

Source: https://habr.com/ru/post/1493317/


All Articles