c# List.Add() 无法添加正确的值

本文关键字:添加 List Add | 更新日期: 2023-09-27 18:33:10

程序假设在面板1上绘制形状。这是我的主窗体的代码:

namespace DrawShapes
{
    public partial class Form1 : Form
    {
        List<Shape> myShapeList;
        Shape shape;
        public Form1()
        {
            InitializeComponent();
        }
        public void AddShape(Shape myshape)
        {
            myShapeList.Add(shape);
        }
        public List<Shape> MyShapeList
        {
            get { return myShapeList; }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            myShapeList = new List<Shape>();
            shape = new Shape();
        }
        private void drawMeButton_Click(object sender, EventArgs e)
        {
            EditShape editShape = new EditShape();
            editShape.Shape = shape;
            if (editShape.ShowDialog() == DialogResult.OK)
            {
                this.shape = editShape.Shape;
                myShapeList.Add(shape);
                panel1.Invalidate();
            }
            editShape.Dispose();
        }
        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            int panelWidth = panel1.ClientRectangle.Width;
            int panelHeight = panel1.ClientRectangle.Height;
            Pen penLine = new Pen(Color.Blue, 1);
            Graphics g = e.Graphics;
            if (myShapeList != null)
            {
                foreach (Shape element in myShapeList)
                {
                    label1.Text = element.Width.ToString();
                    g.DrawRectangle(penLine, element.XCordinates, element.XCordinates, 50, 50); 
                }
            }
        } 
    }
}

这是"我的编辑形状"对话框的代码

namespace DrawShapes
{
    public partial class EditShape : Form
    {
        Shape shape = null;

        public EditShape()
        {
            InitializeComponent();
        }
        public Shape Shape
        {
            get { return shape; }
            set { shape = value; }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            shape.Width = 50;
            shape.Height = 50;
            shape.XCordinates = int.Parse(textBox1.Text);
            shape.YCordinates = int.Parse(textBox2.Text);
            shape.Type = 0;
            DialogResult = DialogResult.OK;
        }
    }
}

我在将形状对象(从编辑形状表单)分配给 myShapeList 时遇到问题,由于某种原因,所有属性都设置为 0。请帮忙。

c# List.Add() 无法添加正确的值

也许问题出在你AddShape方法上。您似乎每次都添加shape而不是传递到方法中的形状(myshape)。

如果你这样做会发生什么?

public void AddShape(Shape myshape)
{
    myShapeList.Add(myshape); // myshapeinstead of shape
}

您似乎实际上并没有将形状添加到形状列表中。你正在接受myShape,然后尝试添加形状。这应该是一个智能错误。

public void AddShape(Shape myshape)
    {
        myShapeList.Add(shape);
    }

编辑:没关系,它不会出现错误,因为您有一个名为 shape 的成员变量。所有内容都为零,因为它调用了形状的默认构造函数。

最大的问题在于您对编辑表单的调用。您正在向列表中添加相同的对象引用。因此,当您修改编辑表单中的引用时,您将添加到列表中的所有元素更改为最新调用中设置的输入

将代码更改为

private void drawMeButton_Click(object sender, EventArgs e)
{
    using(EditShape editShape = new EditShape())
    {
        // Here, create a new instance of a Shape and edit it....
        editShape.Shape = new Shape();
        if (editShape.ShowDialog() == DialogResult.OK)
        {
            // Add the new instance to the list, not the same instance
            // declared globally. (and, at this point, useless)
            myShapeList.Add(editShape.Shape);
            panel1.Invalidate();
        }
        // The using blocks makes this call superflous
        // editShape.Dispose();
    }
}

那么当你调用方法AddShape(Shape myshape)时就不清楚了,但很明显你在该方法中有拼写错误

我发现了我的错误。我使用错误的参数创建了新的事件处理程序。因此,假设由我的对话框"确定"按钮传递的信息从未正确分配。愚蠢的错误。谢谢大家。