Search code examples
delphiswitch-statementdelphi-10-seattle

How to create multiple conditions in Case command?


Ex:

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

    end;

Solution

  • Use commas to separate the labels:

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

    As @SertacAkyuz mentioned, if the values are consecutive, you can do something like:

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