Delphi – 以字串做為 Case 選項

unit unit1;

interface

uses
  Windows, Messages....

type
  TFormName = (Casefm1, Casefm2, Casefm3);

function xxx(): Boolean;
var
  StrFormName: String;
  CaseFormName: TFormName;
begin
  StrFormName := 'Case' + vFormName; 
  // 將 FormName(物件) 與 TFormName(值) 做區隔。
  // (vFormName為帶有各FormName的字串變數)
  CaseFormName := TFormName(GetEnumValue(TypeInfo(TFormName), StrFormName));

  case CaseFormName of
    Casefm1:
      begin
        ..............
      end;

    Casefm2:
      begin
        ..............
      end;

    Casefm3: 
      begin
       ...............
      end;

【參考連結】

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.