如何使用键盘箭头键向上,向下,向右和向左移动标签位置 [英] How to move label position up,down,right and left using keyboard arrow keys

查看:631
本文介绍了如何使用键盘箭头键向上,向下,向右和向左移动标签位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们,



我有一个标签控件我想用键盘箭头键在表格上移动这个标签控件。

第二个问题

我在窗体中有4个按钮来移动控件。我已经为此编写了代码,但是当用户点击按钮而没有释放时,如何处理点击控件应朝着方向移动。(避免多次点击移动控件)



我的代码如下

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

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);
     }

推荐答案

KeyDown 事件:

Add a handler for the KeyDown event:
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;
    }
}



如果你想要重点关注表单的控件而不是表单本身,您需要将 KeyPreview 属性设置为true。您可以通过Visual Studio中的Windows窗体设计器执行此操作,也可以将其添加到构造函数中:


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:

this.KeyPreview = true;


使用Label响应箭头键: 这很棘手,因为Label不像其他控件那样表现得好:例如,你不能选中它。去年,我为处理Arrow Keys写了一个很长的解决方案,这里:[ ^ ]。看看移动箭头键是否有帮助;如果没有,请随时在这里留下反馈,或者提出进一步的问题。



按钮移动标签:



在下面的代码中,移动Label的四个按钮都连接到相同的MouseDown和MouseUp EventHandler。在MouseDown EventHandler中,根据单击的Button分配控制Label移动方向的值。然后,启动一个Timer,在每个'Tick事件中移动Label。
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;

// 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();
}

在Timer中,记录Label的当前位置,然后移动它:然后进行测试以查看Label在其包含的Form中是否仍然完全可见,或ContainerControl;如果标签不完全可见,则将其移回到移动前的位置,并停止计时器。

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();
    }
}


这篇关于如何使用键盘箭头键向上,向下,向右和向左移动标签位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆