How to prevent code repetition through events

I am a novice programmer and I feel like I am repeating code unnecessarily. I want to make a picture puzzle game consisting of 16 pictures. The problem is that I feel that I have to repeat the code for each event at the checkout, as in the example below:

       Point move;
    bool isDragging = false;
    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
        isDragging = true;
        move = e.Location;
    }

    private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {

        if(isDragging == true)
        {
            pictureBox1.Left += e.X - move.X;
            pictureBox1.Top += e.Y - move.Y;
            pictureBox1.BringToFront();
        }

    }

    private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
    {
        isDragging = false;
    }
+4
source share
4 answers

Just create one method for each of the three events:

private void pictureBox_MouseDown(object sender, MouseEventArgs e)
{
    isDragging = true;
    move = e.Location;
}

private void pictureBox_MouseMove(object sender, MouseEventArgs e)
{

    if(isDragging == true)
    {
        // Luckily the sender parameter will tell us which PictureBox we are dealing with
        PictureBox pb = (PictureBox)sender;
        pb.Left += e.X - move.X;
        pb.Top += e.Y - move.Y;
        pb.BringToFront();
    }

}

private void pictureBox_MouseUp(object sender, MouseEventArgs e)
{
    isDragging = false;
}

16 MouseUp, pictureBox_MouseUp MouseMove, pictureBox_MouseMove MouseDown, pictureBox_MouseMove. 16 .

+8

PictureBox

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
    isDragging = true;
    move = e.Location;
}

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
    PictureBox pictureBox = sender as PictureBox;
    if(isDragging == true)
    {
        pictureBox .Left += e.X - move.X;
        pictureBox .Top += e.Y - move.Y;
        pictureBox .BringToFront();
    }

}

private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
    isDragging = false;
}
+1

, , -. . ,

public partial class DraggablePictureBox : UserControl
{
    public DraggablePictureBox()
    {
        InitializeComponent();
    }

    /// <summary>
    /// Sets image of inner picture
    /// </summary>
    public Image Image
    {
        get {
            return InnerPictureBox.Image;
        }
        set
        {
            InnerPictureBox.Image = value;
        }
    }

    private void InnerPictureBox_MouseMove(object sender, MouseEventArgs e)
    {
        if (isDragging == true)
        {
            this.Left += e.X - move.X;
            this.Top += e.Y - move.Y;
            this.BringToFront();
        }

    }

    private void InnerPictureBox_MouseUp(object sender, MouseEventArgs e)
    {
        isDragging = false;
    }

    private void InnerPictureBox_MouseDown(object sender, MouseEventArgs e)
    {
        isDragging = true;
        move = e.Location;
    }

    private Point move;
    private bool isDragging = false;
}

.

enter image description here

enter image description here

+1

,

MovePicturePox(Point move, Point newPos, PictureBox pb)
{
    pb.Left += newPos.X - move.X;
    pb.Top += newPos.Y - move.Y;
    pb.BringToFront();
}

:

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
    if(isDragging == true)
    {
        MovePictureBox(move, new Point(e.X, y.Y), pictureBox1);
    }

}
0

All Articles