C# switch case

The switch statement provides multiple cases for selection. Following is an example for using switch to handle different keyboard press cases.

void AlignmentView_KeyDown(object sender, KeyEventArgs kea)
{
switch (kea.KeyData)
{
case Keys.Left:
int xPos = caret.physicalPosition.X / ftWidth - 1;
int yPos = caret.physicalPosition.Y / ftHeight-2;
MoveCaret(xPos, yPos);
UpdateCaret();
kea.Handled = true;
break;
case Keys.Right:
int x = caret.physicalPosition.X / ftWidth + 1;
int y = caret.physicalPosition.Y / ftHeight-2;
MoveCaret(x, y);
UpdateCaret();
kea.Handled = true;
break;
case Keys.Up:
...
break;
case Keys.Down:
...
break;
case Keys.Delete:
...
break;
case Keys.Back:
...
break;
case Keys.Insert:
...
break;
default:
...
break;
}
}



endmemo.com © 2017  Terms of Use | Home