0

Hi guys and sorry if this is the wrong forum. I am messing around with visual studio and trying to make a picturebox move around on a form when a user clicks a directional button on the form. I am able to move the picturebox any direction I want by doing something like this:

private void downBut_Click(object sender, EventArgs e)
        {
            Point p = moveBox.Location;
            p.Y += 20;
            moveBox.Location = p;

        }

However, I would like to put the meat of the method into another class called movementClass and handle movement that way. For example:

private void leftBut_Click(object sender, EventArgs e)
        {
            movementClass MC = new movementClass();
            MC.moveLeft();
        }

Would call:

class movementClass:Form1
    {

        public void moveLeft()
        {
            Point p = moveBox.Location;
            p.X -= 20;
            moveBox.Location = p;

        }

I don't get any errors right now when running the program, but nothing happens when I click the button to move the picturebox to the left. Can someone point me in the right direction?

2
Contributors
2
Replies
12
Views
3 Years
Discussion Span
Last Post by bigredaltoid
1

movementClass.cs:

Add the following "using" statements:

using System.Drawing; and
using System.Windows.Forms;

public static class movementClass 
{
    public static void moveLeft(PictureBox moveBox)
    {
        Point p = moveBox.Location;
        p.X -= 20;
        moveBox.Location = p;
    }

    public static void moveDown(PictureBox moveBox)
    {
        Point p = moveBox.Location;
        p.Y += 20;
        moveBox.Location = p;
    }
}

Then in Form1.cs:

private void downBut_Click(object sender, EventArgs e)
{
    movementClass.moveDown(moveBox);
}

private void leftBut_Click(object sender, EventArgs e)
{
    movementClass.moveLeft(moveBox);
}

Edited by cgeier

0

xD I can't believe it was something as simple as that. Ah well, lesson learned. Thank you very much.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.