无法使用箭头键移动

鹰眼

成员
加入
2015年11月13日
消息
13
编程经验
Beginner
我正在尝试使用左边,右转,向上移动我的表格&键盘上的钥匙,但没有发生任何事情。

这是我正在尝试的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace SC1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left) { this.Left += e.X - lastPoint.X; this.Top += e.Y - lastPoint.Y; }
        }


        Point lastPoint;
        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            lastPoint = new Point(e.X, e.Y);
        }


        //Move form to the Right
        private void RightButton_Click(object sender, EventArgs e)
        {
            this.Location = new Point(this.Location.X + 10, this.Location.Y);
}
        //Move form to the left:
        private void LeftButton_Click(object sender, EventArgs e)
        {
             this.Location = new Point(this.Location.X - 10, this.Location.Y);
        }
  
        //Move form up:
        private void UpButton_Click(object sender, EventArgs e)
        {
             this.Location = new Point(this.Location.X, this.Location.Y - 10);
        }  


        //Move form down:
        private void DownButton_Click(object sender, EventArgs e)
        {
            this.Location = new Point(this.Location.X, this.Location.Y + 10);
        }
    }
}
 
Last edited:

鹰眼

成员
加入
2015年11月13日
消息
13
编程经验
Beginner
问题解决了!

这就是代码应该如何看法:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace SC1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left) { this.Left += e.X - lastPoint.X; this.Top += e.Y - lastPoint.Y; }
        }


        Point lastPoint;
        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            lastPoint = new Point(e.X, e.Y);
        }


        private void button1_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }


        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.Up)
            {
                Top -= 1;
                return true;
            }
            if (keyData == Keys.Down)
            {
                Top += 1;
                return true;
            }
            if (keyData == Keys.Left)
            {
                Left -= 1;
            }
            if (keyData == Keys.Right)
            {
                Left += 1;
                return true;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }
    }
}
 
最后编辑了主持人:

juggalobrotha

工作人员
加入
2011年4月23日
消息
177
地点
兰辛,MI;美国
编程经验
10+
我正在尝试使用左边,右转,向上移动我的表格&键盘上的钥匙,但没有发生任何事情。

这是我正在尝试的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace SC1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left) { this.Left += e.X - lastPoint.X; this.Top += e.Y - lastPoint.Y; }
        }


        Point lastPoint;
        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            lastPoint = new Point(e.X, e.Y);
        }


        //Move form to the Right
        private void RightButton_Click(object sender, EventArgs e)
        {
            this.Location = new Point(this.Location.X + 10, this.Location.Y);
}
        //Move form to the left:
        private void LeftButton_Click(object sender, EventArgs e)
        {
             this.Location = new Point(this.Location.X - 10, this.Location.Y);
        }
  
        //Move form up:
        private void UpButton_Click(object sender, EventArgs e)
        {
             this.Location = new Point(this.Location.X, this.Location.Y - 10);
        }  


        //Move form down:
        private void DownButton_Click(object sender, EventArgs e)
        {
            this.Location = new Point(this.Location.X, this.Location.Y + 10);
        }
    }
}
您无法处理表单的键盘事件,这是您检查4箭头键并适当地移动表格。只需务必将表单的keypreview设置为true。
namespace ArrowKeysForm {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }

        private void upButton_Click(object sender, EventArgs e) {
            this.Top -= 10;
        }

        private void downButton_Click(object sender, EventArgs e) {
            this.Top += 10;
        }

        private void leftButton_Click(object sender, EventArgs e) {
            this.Left -= 10;
        }

        private void rightButton_Click(object sender, EventArgs e) {
            this.Left += 10;
        }

        private void Form1_KeyUp(object sender, KeyEventArgs e) {
            switch (e.KeyCode) {
                case Keys.Up:
                    upButton.PerformClick();
                    break;
                case Keys.Down:
                    downButton.PerformClick();
                    break;
                case Keys.Left:
                    leftButton.PerformClick();
                    break;
                case Keys.Right:
                    rightButton.PerformClick();
                    break;
            }
        }
    }
}
 

鹰眼

成员
加入
2015年11月13日
消息
13
编程经验
Beginner
您无法处理表单的键盘事件,这是您检查4箭头键并适当地移动表格。只需务必将表单的keypreview设置为true。
namespace ArrowKeysForm {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }

        private void upButton_Click(object sender, EventArgs e) {
            this.Top -= 10;
        }

        private void downButton_Click(object sender, EventArgs e) {
            this.Top += 10;
        }

        private void leftButton_Click(object sender, EventArgs e) {
            this.Left -= 10;
        }

        private void rightButton_Click(object sender, EventArgs e) {
            this.Left += 10;
        }

        private void Form1_KeyUp(object sender, KeyEventArgs e) {
            switch (e.KeyCode) {
                case Keys.Up:
                    upButton.PerformClick();
                    break;
                case Keys.Down:
                    downButton.PerformClick();
                    break;
                case Keys.Left:
                    leftButton.PerformClick();
                    break;
                case Keys.Right:
                    rightButton.PerformClick();
                    break;
            }
        }
    }
}

谢谢Juggalobrotha,

我会补充一点。
 
最佳 底部