开发者

How to get enumeration type into stringlist?

开发者 https://www.devze.com 2022-12-14 09:33 出处:网络
This single line of code: ShowMessage(GetEnumName(TypeInfo(TAlign), 1)); returns \"alTop\". How can I get all values of enumerated type into stringlist, when I want to use string variable: \'TAli

This single line of code:

ShowMessage(GetEnumName(TypeInfo(TAlign), 1));

returns "alTop".

How can I get all values of enumerated type into stringlist, when I want to use string variable: 'TAlign' instead of TAlign? Something like:

ShowMessage(GetEnumName(Ty开发者_如何学GopeInfo('TAlign'), 1));

Thanx


To be able to use a string variable, you'd need to register the TypeInfo with the string in some sort of lookup table, and then look it up.

To get all the enumerated type names in your list, you can do something like this:

procedure LoadAllEnumValuesIntoStringList(enum: PTypeInfo; list: TStringList);
var
   data: PTypeData;
   i: integer;
begin
   list.clear;
   data := GetTypeData(GetTypeData(enum)^.BaseType^);
   for i := 0 to data.MaxValue do
      list.add(GetEnumName(enum, i));
end;
0

精彩评论

暂无评论...
验证码 换一张
取 消