How to move picturebox with arrow keys with listbo

2019-06-10 02:40发布

Every things is ok form1+picturebox1 i can use good below codes:


   public Form1()
        {
            InitializeComponent();
            KeyDown += new KeyEventHandler(Form1_KeyDown);
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            int x = pictureBox1.Location.X;
            int y = pictureBox1.Location.Y;

            if (e.KeyCode == Keys.Right) x += 5;
            else if (e.KeyCode == Keys.Left) x -= 5;
            else if (e.KeyCode == Keys.Up) y -= 5;
            else if (e.KeyCode == Keys.Down) y += 5;

            pictureBox1.Location = new Point(x, y);

        }

BUT; if i add listbox to write position picturebox, i can not move picturebox with arrow keys how can i do that?

2条回答
beautiful°
2楼-- · 2019-06-10 02:49

You need to Invalidate which will redraw your UI; In order to see those changes that you made.

You can choose to redraw certain regions with the overload Invalidate(Region region) or you can invalidate the whole control by doing Control.Invalidate().

Try addign: this.Invalidate(); after you've created the new Point.

You can also use Refresh() which will invalidate and force the control to redraw.

Here is an MSDN Article about "How to: Handle Keyboard input at Form Level" you might want to read that and compare it to your current code. You might want to change KeyDown to KeyPress instead. And also as I said in my comment, it really should work without forcing the form to redraw using Invalidate() or Refresh().

查看更多
孤傲高冷的网名
3楼-- · 2019-06-10 02:52

Probalby your listbox gets the focus an not the form, you probably have to listen the listboxs keydown-event.

EDIT:

Use this focusable PictureBoxEx and listen to its previewkeydown-event :

public class PictureBoxEx : PictureBox
{
    public PictureBoxEx()
    {
        this.SetStyle(ControlStyles.Selectable, true);
    }

    protected override void OnClick(EventArgs e)
    {
        this.Focus();
        base.OnClick(e);
    }
}

EDIT:

Or use the following. with this you don't have to move a control in the form, but just move the image within the control.

public class PictureBoxEx : Control
    {
        public PictureBoxEx()
        {
            this.SetStyle(ControlStyles.Selectable | ControlStyles.AllPaintingInWmPaint | ControlStyles.ResizeRedraw | ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer, true);


        }


        protected override void OnClick(EventArgs e)
        {
            this.Select();
            base.OnClick(e);
        }

        private Image _Image;
        public Image Image
        {
            get
            {
                return _Image;
            }
            set
            {
                _Image = value;
                this.Invalidate();
            }
        }

        private Point _ImageLocation = new Point(0,0);
        public Point ImageLocation
        {
            get
            {
                return _ImageLocation;
            }
            set
            {
                _ImageLocation = value;
                this.Invalidate();
            }
        }

        private int _ImageLocationLeft = 0;
        public int ImageLocationLeft
        {
            get
            {
                return _ImageLocationLeft;
            }
            set
            {
                _ImageLocationLeft = value;
                ImageLocation = new Point(value, ImageLocationTop);
            }
        }

        private int _ImageLocationTop = 0;
        public int ImageLocationTop
        {
            get
            {
                return _ImageLocationTop;
            }
            set
            {
                _ImageLocationTop = value;
                ImageLocation = new Point(ImageLocationLeft, value);
            }
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            if (Image != null)
            {
                pe.Graphics.DrawImage(Image, ImageLocation);
            }
            base.OnPaint(pe);
        }

        protected override void OnPreviewKeyDown(PreviewKeyDownEventArgs e)
        {
            if (e.KeyData == Keys.Up || e.KeyData == Keys.Down || e.KeyData == Keys.Left || e.KeyData == Keys.Right)
            e.IsInputKey = true;
            base.OnPreviewKeyDown(e);
        }
    }
查看更多
登录 后发表回答