有没有办法计算组件内的“True”Tset项目?
我做了什么:
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;
该组件有很多TCollectionItem,所有Items都有不同的Colortypes。 (该组件连接到主窗体上的checklistbox)
例如:
AColorItem
BColorItem
我想算“真实的状态”。如果count是> 1,我想做点什么......
TProperty位于具有Item属性的TCollection中......
我可以用......来实现它。
var
C: integer
vItem: TColorItem
begin
for ...
PropertyCollection.Items[C].ColorTypes
感谢您的帮助!
答案 0 :(得分:3)
您可以通过将其与空集[]
进行比较来检查该集是否为空。您可以使用函数计算项目数(如下所示):
program Project1;
{$APPTYPE CONSOLE}
uses SysUtils;
type
TColorItem = (ms_red, ms_blue, ms_green, ms_yellow);
TColorItems = set of TColorItem;
procedure WriteSetContents(const AColorItems : TColorItems);
begin
WriteLn('Set contains...');
if AColorItems = [] then begin
WriteLn('Nothing');
Exit;
end;
if ms_red in AColorItems then WriteLn('Red');
if ms_blue in AColorItems then WriteLn('Blue');
if ms_green in AColorItems then WriteLn('Green');
if ms_yellow in AColorItems then WriteLn('Yellow');
end;
function GetSetCount(const AColorItems : TColorItems) : integer;
var
ci : TColorItem;
begin
result := 0;
for ci := Low(TColorItem) to High(TColorItem) do
if ci in AColorItems then Inc(result);
end;
var
cis : TColorItems;
begin
cis := [];
WriteLn(Format('Set has %d Items', [GetSetCount(cis)]));
WriteSetContents(cis);
WriteLn;
cis := cis + [ms_red];
WriteLn(Format('Set has %d Items', [GetSetCount(cis)]));
WriteSetContents(cis);
WriteLn;
cis := cis + [ms_green, ms_yellow];
WriteLn(Format('Set has %d Items', [GetSetCount(cis)]));
WriteSetContents(cis);
ReadLn;
end.