繁体   English   中英

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

[英]c# List.Add() unable to add correct values

程序假设在panel1上绘制形状。 这是我主要形式的代码:

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。 请帮忙。

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

如果您改为执行此操作会怎样?

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

看来您实际上并没有在形状列表中添加形状。 您正在使用myShape,然后尝试添加形状。 这应该是一个智能错误。

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

编辑:没关系,它不会出现错误,因为您有一个名为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)时并不清楚,但很明显,该方法中有错字

我发现了我的错误。我用错误的参数创建了新的事件处理程序。 因此,从未正确分配我的对话框“确定”按钮传递的信息。 愚蠢的错误。 多谢你们。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM