首页 > 解决方案 > c#对象参数在分配参数后神秘地清除

问题描述

我是 C# 的新手。我想为MouseUp事件中的 Paint 动作收集所有分数。从aPaintAction2into传入参数后Actions2,我清除aPaintAction2. 不知何故,aPaintAction2内容被清除后,Actions2aPaintAction2传入的)参数值也被清除了。

有人可以向我解释这是什么问题,为什么会这样?我只想将点数aPaintAction2传入Actions2Actions2保持点数参数,并清除aPaintAction2,以便aPaintAction2可以保持新点。谢谢你。

    private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {
        if (moving && x != -1 && y != -1)
        {
            aPaintAction2.Add(e.Location);
            x = e.X;
            y = e.Y;
        }
    }

    private List<AnnotationAction> Actions2 = new List<AnnotationAction>();
    private List<Point> aPaintAction2 = new List<Point>();
    private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
    {
        // save a Paint action
        Actions2.Add(new AnnotationAction(newActionId, pen.Color, pen.Width, aPaintAction2));

        aPaintAction2.Clear();

        moving = false;
        x = -1;
        y = -1;
        newActionId++;
    }

标签: c#listpointmouseup

解决方案


代替

aPaintAction2.Clear();

从列表中删除所有项目,请尝试:

aPaintAction2 = new List<Point>();

这将创建一个新的空列表,但将旧列表保留在 Actions 中。


推荐阅读