delphi 遍历枚举、获取枚举值的名称

遍历枚举、获取枚举值的名称

代码

遍历枚举

uses System.TypInfo;

procedure TForm1.Button1Click(Sender: TObject);
var
  I: TAlign;
begin
  for I := Low(TAlign) to High(TAlign) do
  begin
    Memo1.Lines.Add('名称 ' + GetEnumName(TypeInfo(TAlign), Ord(I)) + ' 值 ' + IntToStr(Ord(I)));
  end;
end;

获取枚举值

uses System.TypInfo;

procedure TForm1.Button2Click(Sender: TObject);
begin
  //获取举类型常量的值
  Memo1.Lines.Add('TAlign中alClient的值' + IntToStr(GetEnumValue(TypeInfo(TAlign), 'alClient')));
  //常量名称不存在时返回-1
  Memo1.Lines.Add('TAlign中ceshi的值' + IntToStr(GetEnumValue(TypeInfo(TAlign), 'ceshi')));
end;

方法

System.TypInfo.GetEnumName

function GetEnumName(TypeInfo: PTypeInfo; Value: Integer): string;

unit

System.TypInfo

将枚举值转换为枚举类型常量的名称。

参数

TypeInfo 描述枚举类型的类型信息记录。 可以通过调用 TypeInfo 函数来获得一个类型的类型信息。

Value 枚举值。

System.TypInfo.GetEnumValue

function GetEnumValue(TypeInfo: PTypeInfo; const Name: string): Integer;

unit

System.TypInfo

将枚举类型常量的名称转换为枚举值。

参数

TypeInfo 描述枚举类型的类型信息记录。 可以通过调用 TypeInfo 函数来获得一个类型的类型信息。

Name 枚举类型常量的名称。匹配不区分大小写。

返回值

返回相应的整数值,如果 Name 与提供的 TypeInfo 中的任何常量不匹配,则返回 -1。

posted @ 2023-05-10 19:17  txgh  阅读(437)  评论(0编辑  收藏  举报