悟生慧

 

C#使用方向键实现控件的移位

void textBox2_KeyDown(object sender,KeyEventArgs e)
        {
            Control ctr= (Control)sender;

            switch (e.KeyData)
            {
                case Keys.Right:
                    if (ctr.Right == 0)
                    { MessageBox.Show("已到顶"); }
                    else
                    { ctr.Location = new Point(ctr.Location.X + 5, ctr.Location.Y + 5); }
                    break;
                case Keys.Up:
                    if (ctr.Top == 0)
                    { MessageBox.Show("已到顶"); }
                    else
                    { //ctr.Top -= 5;
                        ctr.Location = new Point(ctr.Location.X, ctr.Location.Y - 5); 
                    }
                    break;
                case Keys.Down:
                    if (ctr.Bottom == 0)
                    { MessageBox.Show("已到顶"); }
                    else
                    { //ctr.Top += 5; 
                        ctr.Location = new Point(ctr.Location.X, ctr.Location.Y + 5);
                    }
                    break;
                case Keys.Left:
                    if (ctr.Left == 0)
                    { MessageBox.Show("已到顶"); }
                    else
                    { //ctr.Left -= 5; 
                        ctr.Location = new Point(ctr.Location.X - 5, ctr.Location.Y);
                    }
                    break;
            }
            if (e.KeyCode == Keys.Right)
            {
                if (ctr.Top == 0)
                {
                    MessageBox.Show("已到顶");
                }
                else
                {
                    ctr.Top -= 5;
                }
            }
        }

 

posted on 2012-04-27 10:04  悟生慧  阅读(1749)  评论(0编辑  收藏  举报

导航