将一组项添加到checklistbox并在

时间:2016-02-18 16:31:51

标签: delphi delphi-xe delphi-xe7

有没有办法填写并从核对清单箱中获取set项?

我做了什么:

  TColorItem = (ms_red, ms_blue, ms_green, ms_yellow);
  TColorItems = set of TColorItem;

我有一个组件,我可以从TColorItems

中进行选择
 TProperty = class(TCollectionItem)
 private
   FModuleItem: TColorItems;  
   procedure SetColorItem(const Value: TColorItems);    
 published
   property ColorTypes: TColorItems read FColorItem write SetColorItem;

 procedure SetColorItem(const Value: TColorItems);
 begin
   FColorItem := Value;
 end;

在我设置(选中)组件中的项目后,我创建了一个表单。

我的表单如下:

enter image description here

如果我检查核对清单框中的任何项目,我想将Result设为TColorItems集:

  • 如果选中红色绿色,则set必须

    Result := [ms_red, ms_green]

  • 如果蓝色,则选中绿色黄色

    Result := [ms_blue, ms_green, ms_yellow]等..

Result必须采用此[value1, value2]形式;我想在之后使用它。

1 个答案:

答案 0 :(得分:10)

声明与TColorItem类型配对的字符串数组。

const
  ColorItemNames: array [TColorItem] of string = ('Red', 'Blue', 'Green', 'Yellow');

用数组填充TCheckListBox对象。

var
  ci: TColorItem;
begin
  for ci := Low(ColorItemNames) to High(ColorItemNames) do
    CheckListBox1.Items.AddObject(ColorItemNames[ci], TObject(ci));
end;

TColorItems属性的Objects获取值TCheckListBox.Items

var
  i: Integer;
begin
  Result := [];
  for i := 0 to CheckListBox1.Count-1 do begin
    if CheckListBox1.Checked[i] then
      Include(Result, TColorItem(CheckListBox1.Items.Objects[i]));
  end;
end;