5

在我的 Delphi 表单中,我有一个带有 4 张图片的 ImageList。还有一个称为 ComboBoxComboBox1和一个称为 TImage 的组件Image9

onChange为我的 ComboBox 创建了一个,因为我想做这样的事情:如果选择了 ComboBox 项目 1,则将图像 1 加载到我的 ImageList 中。如果选择了 ComboBox 项目 3(例如),同样的情况,加载 ImageList 的图像 3。

我写的代码是这样的:

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;

使用此代码,IDE(我使用的是 Delphi XE4)给我一个错误,case ComboBox1.Items[ComboBox1.ItemIndex] of因为它说需要一个 Ordinal 类型。我能做什么?

4

1 回答 1

10

caseDelphi 中的语句适用于序数类型

序数类型包括整数、字符、布尔值、枚举和子范围类型。序数类型定义了一组有序值,其中除了第一个值之外的每个值都有一个唯一的前任,而除了最后一个之外的每个值都有一个唯一的后继。此外,每个值都有一个序数,它决定了类型的顺序。在大多数情况下,如果一个值的序数为 n,则其前一个值的序数为 n-1,其后续值的序数为 n+1

ComboBox.Items是字符串,因此不满足作为序数的要求。

此外,正如您在下面的评论中所述,您不能Image9.Picture直接分配给您;你必须Image9.Picture.Bitmap改用。为了TImage正确更新以反映更改,您需要调用它的Invalidate方法。)

改为直接case使用ItemIndex

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

或者直接去ImageList

if ComboBox1.ItemIndex <> -1 then
begin
  ImageList1.GetBitmap(ComboBox1.ItemIndex, Image9.Picture.Bitmap);
  Image9.Invalidate;
end;
于 2013-07-24T22:11:28.730 回答