Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
205 views
in Technique[技术] by (71.8m points)

c# - Controling the Moving PictureBox

So, I am kinda stucked on my moving PICTURE box; at the moment my form 2 loads the picture box starts moving, that is exactly what I wanted, However it isn't in my Control and that's what I am trying to do, but I've failed.....All I want is the direction of the moving picture Box to be controlled by the keys......Thanks.

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

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {

        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            timer1.Start();
        }

        private void Form2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Right) 
            {
                pictureBox1.Location = new Point(pictureBox1.Location.X + 5, pictureBox1.Location.Y);
            }

            if (e.KeyCode == Keys.Up)
            {
                pictureBox1.Location = new Point(pictureBox1.Location.X, pictureBox1.Location.Y - 5);
            }

            if (e.KeyCode == Keys.Left)
            {
                pictureBox1.Location = new Point(pictureBox1.Location.X - 5, pictureBox1.Location.Y);
            }

            if (e.KeyCode == Keys.Down)
            {
                pictureBox1.Location = new Point(pictureBox1.Location.X, pictureBox1.Location.Y + 5);
            }

        }

        private void timer1_Tick(object sender, EventArgs e)
        {

           pictureBox1.Location = new Point(pictureBox1.Location.X + 25, pictureBox1.Location.Y);
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need a variable that indicates in which direction the box is moving:

    private enum Direction { None, Left, Right, Up, Down };
    private Direction currentDir = Direction.None;

The timer's Tick event needs to check this variable to know in which direction to move the box:

    private void timer1_Tick(object sender, EventArgs e) {
        int vel = 5;
        switch (currentDir) {
            case Direction.Left:  pictureBox1.Left -= Math.Min(vel, pictureBox1.Left); break;
            case Direction.Right: pictureBox1.Left += Math.Min(vel, this.ClientSize.Width - pictureBox1.Right); break;
            case Direction.Up:    pictureBox1.Top  -= Math.Min(vel, pictureBox1.Top); break;
            case Direction.Down:  pictureBox1.Top  += Math.Min(vel, this.ClientSize.Height - pictureBox1.Bottom); break;
        }
    }

Your KeyDown event handler should simply set the variable:

    private void Form1_KeyDown(object sender, KeyEventArgs e) {
        switch (e.KeyData) {
            case Keys.Left:  currentDir = Direction.Left; break;
            case Keys.Right: currentDir = Direction.Right; break;
            case Keys.Up:    currentDir = Direction.Up; break;
            case Keys.Down:  currentDir = Direction.Down; break;
        }
    }

You also need the KeyUp event, it should stop moving the box again:

    private void Form1_KeyUp(object sender, KeyEventArgs e) {
        switch (e.KeyData) {
            case Keys.Left:  if (currentDir == Direction.Left)  currentDir = Direction.None; break;
            case Keys.Right: if (currentDir == Direction.Right) currentDir = Direction.None; break;
            case Keys.Up:    if (currentDir == Direction.Up)    currentDir = Direction.None; break;
            case Keys.Down:  if (currentDir == Direction.Down)  currentDir = Direction.None; break;
        }
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

Just Browsing Browsing

[6] html - How to create even cell spacing within a

1.4m articles

1.4m replys

5 comments

57.0k users

...