0

I'm struggling to set the values of an element at a specific index within a list to a new value.

The list is of type object Rectangle and i get the following error when I try change any of the values of the rectangle in the list e.g.

Property or indexer 'System.Drawing.Rectangle.Bottom' cannot be assigned to -- it is read only

I've tried converting the list to an array but i still run into the same issue of the values being read-only.

Basically the app takes in a user defined number of rectangles and draws the rectangles with varying widths and heights but along the same baseline. The code im trying to implement needs to take those rectangles and redraw them vertically from the base upwards, while keeping the same number of rectangles and keeping the same outer shape as the previous rectangles created.

Code:

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

The parts that are giving the errors are:

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

Please can someone help me out or at least direct me in the right direction

4

3 に答える 3

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] 

そのため、長方形の値をリストに保存する必要があります

于 2012-04-15T13:20:43.177 に答える
2

プロパティ 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 );
于 2012-04-15T11:05:22.913 に答える
1
Rectangles[i] = new Rectangle(Rectangles[t].Left, Rectangles[i].Top, Rectangles[i].Width, Rectangles[i].Height);

必要なインデックスに新しい四角形を割り当てます。

于 2012-04-15T11:05:24.237 に答える