如何在Case命令中创建多个条件?

时间:2019-04-16 17:42:53

标签: delphi switch-statement delphi-10-seattle

例如:

var
 Msg: Cardinal;
case Msg of
      WM_CHAR:
      WM_KEYDOWN:
      WM_KEYUP:
        begin
         // Do something
        end;

    end;

2 个答案:

答案 0 :(得分:6)

使用逗号分隔标签:

var
  Msg: Cardinal;
... 
case Msg of
  WM_CHAR,
  WM_KEYDOWN,
  WM_KEYUP:
    begin
      // Do something
    end;
end;

如@SertacAkyuz所述,如果值是连续的,则可以执行以下操作:

case Msg of
  WM_KEYDOWN .. WM_CHAR: // range 
    begin
      // Do something
    end;
end;

答案 1 :(得分:0)

假设您希望在所有三种情况下都执行“做某事”,则必须用逗号分隔个案标签:

var
  Msg: Cardinal;

case Msg of
  WM_CHAR,
  WM_KEYDOWN,
  WM_KEYUP:
        begin
         // Do something
        end;
end;