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

问题描述 投票:-1回答:2

例如:

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

    end;
delphi switch-statement delphi-10-seattle
2个回答
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;

0
投票

假设您希望在所有三种情况下都执行“Do Something”,则必须使用逗号分隔案例标签:

var
  Msg: Cardinal;

case Msg of
  WM_CHAR,
  WM_KEYDOWN,
  WM_KEYUP:
        begin
         // Do something
        end;
end;
© www.soinside.com 2019 - 2024. All rights reserved.