【问题标题】:C# Changing the value of an element in a list of type RectangleC# 更改 Rectangle 类型列表中元素的值
【发布时间】:2012-04-15 10:58:37
【问题描述】:

我正在努力将列表中特定索引处的元素值设置为新值。

列表是对象矩形类型,当我尝试更改列表中矩形的任何值时出现以下错误,例如

属性或索引器“System.Drawing.Rectangle.Bottom”不能 分配给 -- 它是只读的

我尝试将列表转换为数组,但我仍然遇到相同的值是只读的问题。

基本上,该应用会接收用户定义数量的矩形,并绘制具有不同宽度和高度但沿相同基线的矩形。我试图实现的代码需要获取这些矩形并从底部向上垂直重绘它们,同时保持相同数量的矩形并保持与先前创建的矩形相同的外部形状。

代码:

public void RotateRectangles(List<Rectangle> Rectangles, int startPositionX, int userInput, Graphics DrawingSurface)
    {
        Graphics RectangleGraphics = DrawingSurface;

        try
        {
            // loop in reverse to compare one rectangle to all the other rectangles in the vector

            for (int i = Rectangles.Count - 1; i > -1; --i)
            {
                bool mustChange = true;

                for (int t = Rectangles.Count - 1; t > -1; --t)
                {
                    // only compare if the current position in the vector A if different to the position in vector B.
                    if (i > t)
                    {
                        if (mustChange == true)
                        {
                            // If the top Y coordinate of RECT at Position i in vector A is bigger than Y coordinate
                            // at Position t in vector B

                            if (Rectangles[i].Top >= Rectangles[t].Top)
                            {
                                //adjusting points accordingly
                                Rectangles[i].Left = (Rectangles[t].Left);
                                Rectangles[t].Bottom = (Rectangles[i].Top);
                            }
                            else
                            {
                                // If the Y coordinate is not bigger, then we need to stop checking
                                mustChange = false;
                            }
                        }
                    }
                }
            }

            // loop forward to compare one rectangle to all the other rectangles in the vector
            for (int i = 0; i < Rectangles.Count; ++i)
            {
                bool forwardChange = true;

                for (int t = 0; t < Rectangles.Count; ++t)
                {
                    // If the top Y coordinate of RECT at Position i in vector A is bigger than Y coordinate at Position t
                    // in vector B AND the two rectangales touch

                    if (i < t && Rectangles[i].Top <= Rectangles[t].Bottom)
                    {
                        if (forwardChange == true)
                        {

                            // If the top Y coordinate of RECT at Position i in vector A is bigger than Y coordinate at Position t 

                            // in vector B

                            if (Rectangles[i].Top > Rectangles[t].Top)
                            {
                                //adjusting points accordingly
                                Rectangles[i].Right = (Rectangles[t].Right);
                                Rectangles[t].Bottom = (Rectangles[i].Top);
                            }
                            else
                            {
                                // If the Y coordinate is not bigger, then we need to stop checking
                                forwardChange = false;
                                // Addjust the Y position of each rectangle so it does not overlap with the first drawing

                                for (int z = 0; z < Rectangles.Count; ++z)
                                {
                                    Rectangles[z].Top = (250 - Rectangles[z].Top);
                                    Rectangles[z].Bottom = (250 - Rectangles[z].Bottom);
                                }
                            }
                        }
                    }
                }
            }
            for (int z = 0; z < Rectangles.Count; ++z)
            {

                Rectangle DrawRec = myRectangleClass.MyRectangle(Rectangles[z].Left, Rectangles[z].Top, Rectangles[z].Right, Rectangles[z].Bottom);
                RectangleGraphics.DrawRectangle(Pen, DrawRec);

                ReadWrite.writeOutput(Rectangles[z].Left, Rectangles[z].Top, Rectangles[z].Right, Rectangles[z].Bottom);
            }
        }
        catch (Exception e)
        {
        }
    }

给出错误的部分是:

Rectangles[i].Left = (Rectangles[t].Left);
Rectangles[t].Bottom = (Rectangles[i].Top);

Rectangles[i].Right = (Rectangles[t].Right);
Rectangles[t].Bottom = (Rectangles[i].Top);

Rectangles[z].Top = (250 - Rectangles[z].Top);
Rectangles[z].Bottom = (250 - Rectangles[z].Bottom);

请有人帮助我或至少指引我正确的方向

【问题讨论】:

  • 矩形是一个值类型。您需要创建一个新值。

标签: c# list properties readonly


【解决方案1】:

属性 Right、Left、Top、Bottom 是只读的。您可以使用 Offset 和 Inflate 方法以及 Location、Size、Width 和 Height 属性来调整矩形的位置和大小,也可以用新创建的矩形替换现有矩形。

例如

Rectangle ri = Rectangles[i];
Rectangle rt = Rectangles[t];

Rectangle[i] = new Rectangle( rt.Left, ri.Bottom, rt.Height, rt.Width );

【讨论】:

    【解决方案2】:

    问题不在于矩形的值类型,而在于 List[] 运算符返回一个新的值对象。

    List<Rectangle> a; 
    
    a[4].X += 4; // does the same like the following code:
    
    var r = a[4] 
    r.X += 4; // will change r, but not a[4] 
    

    所以您需要将矩形值存储回列表中

    【讨论】:

      【解决方案3】:
      Rectangles[i] = new Rectangle(Rectangles[t].Left, Rectangles[i].Top, Rectangles[i].Width, Rectangles[i].Height);
      

      为想要的索引分配一个新的矩形。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-08
        • 2019-06-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多