Responding to Arrow keys with Label: this is tricky because a Label doesn't quite behave like other Controls: you can't tab to it, for example. I wrote a long "solution" for dealing with Arrow Keys last year, here: [
^]. See if that helps out with moving the Arrow Keys; if it doesn't please feel free to leave feedback here, or ask further questions.
Moving the Label by Button(s):
In the following code the four buttons that move the Label are all wired-up to the same MouseDown and MouseUp EventHandlers. In the MouseDown EventHandler values that control the direction of the movement of the Label are assigned based on which Button was clicked. Then, a Timer is started that moves the Label in each 'Tick event.
private const int LeftMove = -1, RightMove = 1, UpMove = -1, DownMove = 1;
private Point currentLabelLocation;
private bool IsLabelMoving = false;
private int xOffset, yOffset;
private void btnMoveLabelMouseDown_Click(object sender, MouseEventArgs e)
{
Button theButton = sender as Button;
xOffset = 0;
yOffset = 0;
switch (theButton.Name)
{
case "btnMoveLabelLeft":
xOffset = LeftMove;
break;
case "btnMoveLabelRight":
xOffset = RightMove;
break;
case "btnMoveLabelUp":
yOffset = UpMove;
break;
case "btnMoveLabelDown":
yOffset = DownMove;
break;
}
IsLabelMoving = true;
MoveLabelTimer.Start();
}
private void btnMoveLabelMouseUp_Click(object sender, MouseEventArgs e)
{
IsLabelMoving = false;
MoveLabelTimer.Stop();
}
In the Timer, the current location of the Label is recorded, and then it is moved: then a test is done to see if the Label is still fully visible inside its containing Form, or ContainerControl; if the Label is not fully visible, then it's moved back to where it was before the move, and the Timer is stopped.
private void MoveLabelTimer_Tick(object sender, EventArgs e)
{
if (IsLabelMoving)
{
currentLabelLocation = lblMoving1.Location;
lblMoving1.Left += xOffset;
lblMoving1.Top += yOffset;
if (! lblMoving1.Parent.ClientRectangle.Contains(lblMoving1.Bounds))
{
MoveLabelTimer.Stop();
lblMoving1.Location = currentLabelLocation;
}
}
else
{
MoveLabelTimer.Stop();
}
}