delphi – 使用ComboBox在TImage中加载ImageList图片
在我的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:
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; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |