加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

delphi – 使用ComboBox在TImage中加载ImageList图片

发布时间:2020-12-15 09:36:49 所属栏目:大数据 来源:网络整理
导读:在我的Delphi表单中,我有一个包含4张图片的 ImageList.还有一个名为ComboBox1的ComboBox和一个名为Image9的TImage组件. 我为我的ComboBox创建了一个onChange,因为我想做这样的事情:如果选择了ComboBox项1,则在我的ImageList中加载图像1.如果选择了ComboBox
在我的Delphi表单中,我有一个包含4张图片的 ImageList.还有一个名为ComboBox1的ComboBox和一个名为Image9的TImage组件.

我为我的ComboBox创建了一个onChange,因为我想做这样的事情:如果选择了ComboBox项1,则在我的ImageList中加载图像1.如果选择了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)给出了一个错误,例如ComboBox1.Items [ComboBox1.ItemIndex],因为它表示需要Ordinal类型.我能做什么?

解决方法

德尔福 case statements工作于 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是字符串,因此不符合作为序数的要求.

此外,如下面的comment所述,您无法直接分配给Image9.Picture;你必须使用Image9.Picture.Bitmap.为了使TImage能够正确更新以反映更改,您需要将其称为Invalidate方法.)

更改您的案例以直接使用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;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读