delphi 在 case 语句中使用字符串

 非常遗憾 Delphi 的 case 语句不支持字符串, 但我觉得这也可能是基于效率的考量;
如果非要在 case 中使用字符串, 也不是不可以变通, 这里提供了五种方法.

本例效果图:

delphi 在 case 语句中使用字符串_第1张图片

代码文件:
 
  
 Unit1;





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


  TForm1 = (TForm)
    RadioGroup1: TRadioGroup;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    Button6: TButton;
     FormCreate(Sender: TObject);
     Button1Click(Sender: TObject);
     Button2Click(Sender: TObject);
     Button3Click(Sender: TObject);
     Button4Click(Sender: TObject);
     Button5Click(Sender: TObject);
     Button6Click(Sender: TObject);
  ;


  Form1: TForm1;





 TypInfo; 


 TForm1FormCreate(Sender: TObject);

  RadioGroup1ItemsCommaText := ;
  RadioGroup1ItemIndex := ;
;


 TForm1Button1Click(Sender: TObject);

   RadioGroup1ItemIndex 
    : Color := clRed;
    : Color := clYellow;
    : Color := clLime;
    : Color := clBlue;
  ;
;


 TForm1Button2Click(Sender: TObject);

  str: ;

  str := RadioGroup1Items[RadioGroup1ItemIndex];
   Length(str) 
     : Color := clRed;
     : Color := clYellow;
     : Color := clLime;
     : Color := clBlue;
  ;
;


 TForm1Button3Click(Sender: TObject);

  str: ;

  str := RadioGroup1Items[RadioGroup1ItemIndex];
   str[] 
    : Color := clRed;
    : Color := clYellow;
    : Color := clLime;
    : Color := clBlue;
  ;
;


 TForm1Button4Click(Sender: TObject);

  List: TStringList;
  str: ;

  List := TStringListCreate;
  ListText := RadioGroup1ItemsText;

  str := RadioGroup1Items[RadioGroup1ItemIndex];
   ListIndexOf(str) 
    : Color := clRed;
    : Color := clYellow;
    : Color := clLime;
    : Color := clBlue;
  ;

  ListFree;
;



  TMyEnum = (a, bb, ccc, dddd);

 TForm1Button5Click(Sender: TObject);

  MyEnum: TMyEnum;
  str: ;

  str := RadioGroup1Items[RadioGroup1ItemIndex];

  MyEnum := TMyEnum(GetEnumvalue(TypeInfo(TMyEnum), str));
   MyEnum 
    a    : Color := clRed;
    bb   : Color := clYellow;
    ccc  : Color := clLime;
    dddd : Color := clBlue;
  ;
;


 TForm1Button6Click(Sender: TObject);

  str: ;

  str := RadioGroup1Items[RadioGroup1ItemIndex];
   CompareStr(str, ) 
    : Color := clRed;
    : Color := clYellow;
    : Color := clLime;
    : Color := clBlue;
  ;
;

.
窗体文件:
 
  
 Form1: TForm1
  Left = 
  Top = 
  Caption = 
  ClientHeight = 
  ClientWidth = 
  Color = clBtnFace
  FontCharset = DEFAULT_CHARSET
  FontColor = clWindowText
  FontHeight = -
  FontName = 
  FontStyle = []
  OldCreateOrder = False
  Position = poDesktopCenter
  OnCreate = FormCreate
  PixelsPerInch = 
  TextHeight = 
   Button1: TButton
    Left = 
    Top = 
    Width = 
    Height = 
    Caption = 
    TabOrder = 
    OnClick = Button1Click
  
   Button2: TButton
    Left = 
    Top = 
    Width = 
    Height = 
    Caption = 
    TabOrder = 
    OnClick = Button2Click
  
   Button3: TButton
    Left = 
    Top = 
    Width = 
    Height = 
    Caption = 
    TabOrder = 
    OnClick = Button3Click
  
   RadioGroup1: TRadioGroup
    Left = 
    Top = 
    Width = 
    Height = 
    Caption = 
    TabOrder = 
  
   Button4: TButton
    Left = 
    Top = 
    Width = 
    Height = 
    Caption = 
    TabOrder = 
    OnClick = Button4Click
  
   Button5: TButton
    Left = 
    Top = 
    Width = 
    Height = 
    Caption = 
    TabOrder = 
    OnClick = Button5Click
  
   Button6: TButton
    Left = 
    Top = 
    Width = 
    Height = 
    Caption = 
    TabOrder = 
    OnClick = Button6Click
  

你可能感兴趣的:(delphi)