Click here to Skip to main content
15,883,705 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi Friends,

I have a label control i want to move this label control across the form with keyboard arrow keys.
second question
I have 4 buttons in the forms to move the control. i have written the code for that but how to handle when the user click on a button and not released the click control should move in the direction.(Avoid multiple clicks to move the control)

My code as follows
C#
private void btntop_Click(object sender, EventArgs e)
     {
         label3.Location = new Point(label3.Location.X, label3.Location.Y - 1);
     }

     private void btndown_Click(object sender, EventArgs e)
     {
         label3.Location = new Point(label3.Location.X, label3.Location.Y +1);
     }

     private void btnright_Click(object sender, EventArgs e)
     {
         label3.Location = new Point(label3.Location.X+1, label3.Location.Y);
     }

     private void btnleft_Click(object sender, EventArgs e)
     {
         label3.Location = new Point(label3.Location.X - 1, label3.Location.Y);
     }
Posted

Add a handler for the KeyDown event:
C#
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    switch (e.KeyCode)
    {
        case Keys.Up:
            label3.Location = new Point(label3.Location.X, label3.Location.Y - 1);
            break;
        case Keys.Down:
            label3.Location = new Point(label3.Location.X, label3.Location.Y + 1);
            break;
        case Keys.Right:
            label3.Location = new Point(label3.Location.X + 1, label3.Location.Y);
            break;
        case Keys.Left:
            label3.Location = new Point(label3.Location.X - 1, label3.Location.Y);
            break;
    }
}

If you also want to make this work if the focus is on a control of the form but not on the form itself, you'll need to set the KeyPreview property to true. You can either do that through the Windows Forms designer in Visual Studio, or by adding this to your constructor:
C#
this.KeyPreview = true;
 
Share this answer
 
Comments
jinesh sam 9-Nov-14 14:00pm    
Code works... but the problem is when i use arrow keys inside a textbox the label moves how to prevent this
Thomas Daniels 9-Nov-14 14:43pm    
Then you have to remove the KeyPreview = true line. But then you might have the problem that it does not move when you want it to move.
jinesh sam 9-Nov-14 14:52pm    
In textbox_enter event i made KeyPreview = false then its work. thanks for your solution :)
Thomas Daniels 10-Nov-14 4:37am    
You're welcome! Note that, if you make it false in TextBox_Enter, you might need to set it back to True when you leave the textbox. So I suggest to set it back to True in the TextBox_Leave event.
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.
C#
private const int LeftMove = -1, RightMove = 1, UpMove = -1, DownMove = 1;

// edit: added missing variable declaration
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.
C#
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();
    }
}
 
Share this answer
 
v2
Comments
jinesh sam 9-Nov-14 14:16pm    
where to declare currentLabelLocation
BillWoodruff 9-Nov-14 16:34pm    
private Point currentLabelLocation; // can be added anywhere in Form scope

I've added it into the code in the example; left it out by accident.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900