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?

Recommended Answers

All 2 Replies

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);
}

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.