如何使用事件避免重复代码

4

我是一名初学者程序员,感觉自己在重复编写代码。我想制作一个由16个图片框组成的拼图游戏。问题在于,我感觉每个图片框的事件都需要重复编写代码,就像以下示例一样:

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

你可以添加一个帮助方法,将 pictureBoxX 和其他必要的变量(例如 e.Xmove.X)作为参数检索,并在每个 pictureBoxX_... 事件函数中调用它。 - RhinoDevel
2
你可以将它们全部指向同一组方法,并使用“sender”来确定哪个图片框。 - juharr
2
这个问题已经有了很好的答案。只是想说把 IsDragging == true 拿出来。在 if 语句的括号内部的表达式是一个布尔表达式。无论你在括号里放什么,都会被转换成 true 或者 falsetrue == true 的结果是 true,而 false == true 的结果是 false。或者换句话说,== true 部分没有任何作用,所以你可以直接写成 if(IsDragging) - Richard Irons
@RichardIrons,这是个不错的主意。 - behnam
4个回答

8
只需为您的三个事件中的每个创建一个方法:
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;
}

接下来,进入设计器中的每个图片框,将MouseUp事件处理程序设置为指向pictureBox_MouseUp,将MouseMove事件处理程序设置为指向pictureBox_MouseMove,将MouseDown事件处理程序设置为指向pictureBox_MouseDown。对于这16个图片框都要这样做。


1
尝试为所有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
创建一个新组件,包含Image组件并将其设置为父容器。将拖放代码编写到新组件中。 例如,
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


你知道一个好的资源来处理用户控件吗? - Martin

0

应对这种情况的一种方法是将它们放在方法中,例如

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

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接