枚举方式 Case of 判断英文字符串

 
//对汉字无效
 
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    ComboBox1: TComboBox;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation
uses TypInfo;
{$R *.dfm}

type
  TMyEnum = (Red, black, Green);
procedure TForm1.Button1Click(Sender: TObject);
var
  MyEnum: TMyEnum;
  str: String;
begin
  str := ComboBox1.Text;//输入Red Black Green
  MyEnum := TMyEnum(GetEnumvalue(TypeInfo(TMyEnum), str));
  case MyEnum of
    Red   :Self.Caption := '红色';
    black :Self.Caption := '黑色';
    Green :Self.Caption := '蓝色';
  end;
end;
end.
粘贴自: <http://www.cnpack.org/ >
 




原文地址:https://www.cnblogs.com/xe2011/p/2527317.html