将一组项添加到checklistbox并在之后读取值

Ste*_*e88 2 delphi delphi-xe delphi-xe7

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

我做了什么:

  TColorItem = (ms_red, ms_blue, ms_green, ms_yellow);
  TColorItems = set of TColorItem;
Run Code Online (Sandbox Code Playgroud)

我有一个组件,我可以选择 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;
Run Code Online (Sandbox Code Playgroud)

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

我的表单看起来像这样:

在此输入图像描述

如果我检查清单箱中的任何项目,我想得到ResultTColorItems组:

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

    Result := [ms_red, ms_green]

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

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

Result必须在此[value1, value2]表格; 我想在之后使用它.

fan*_*cco 10

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

const
  ColorItemNames: array [TColorItem] of string = ('Red', 'Blue', 'Green', 'Yellow');
Run Code Online (Sandbox Code Playgroud)

TCheckListBox用数组填充对象.

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

获取的值作为TColorItemsObjects该的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;
Run Code Online (Sandbox Code Playgroud)