How to get an enumeration type to a string list?

This single line of code:

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

returns "alTop".

How can I get all the values โ€‹โ€‹of an enum type in a stringlist when I want to use a string variable: "TAlign" instead of TAlign? Sort of:

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

Thanx

+4
source share
1 answer

To be able to use a string variable, you need to register TypeInfo with the string in some lookup table, and then view it.

To get all of the listed 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; 
+7
source

All Articles