首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >C#更改矩形类型列表中元素的值

C#更改矩形类型列表中元素的值
EN

Stack Overflow用户
提问于 2012-04-15 10:58:37
回答 3查看 2.9K关注 0票数 0

我很难在列表中的特定索引中将元素的值设置为新值。

列表的类型是对象矩形,当我尝试更改列表中矩形的任何值时,我会得到以下错误。

不能将

属性或索引器'System.Drawing.Rectangle.Bottom‘分配给--它是只读的

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

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

代码:

代码语言:javascript
运行
复制
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)
        {
        }
    }

造成错误的部分是:

代码语言:javascript
运行
复制
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);

请有人帮我一下,或者至少把我引向正确的方向。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-04-15 11:05:22

右、左、顶、底的属性是只读的。可以使用偏移和充气方法以及属性位置、大小、宽度和高度来调整矩形的位置和大小,也可以用新创建的矩形替换现有的矩形。

例如:

代码语言:javascript
运行
复制
Rectangle ri = Rectangles[i];
Rectangle rt = Rectangles[t];

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

Stack Overflow用户

发布于 2012-04-15 13:20:43

问题不是矩形的值类型,而是List[]操作符返回一个新的值对象。

代码语言:javascript
运行
复制
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] 

因此,您需要将矩形值存储在列表中。

票数 2
EN

Stack Overflow用户

发布于 2012-04-15 11:05:24

代码语言:javascript
运行
复制
Rectangles[i] = new Rectangle(Rectangles[t].Left, Rectangles[i].Top, Rectangles[i].Width, Rectangles[i].Height);

将一个新矩形分配给所需的索引。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10161400

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档