2012-04-15 85 views
0

我努力將列表中特定索引處元素的值設置爲新值。C#更改矩形類型的列表中的元素的值

該列表的類型爲對象Rectangle,當我嘗試更改列表中矩形的任何值時,出現以下錯誤,例如

屬性或索引「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); 

請能有人幫我,或者至少告訴我在正確的方向

+0

矩形爲*值類型*。您需要創建一個新值。 – 2012-04-15 13:51:43

回答

2

屬性Right,Left,Top,Bottom是隻讀的。您可以使用「偏移」和「充氣」方法以及「位置」,「大小」,「寬度」和「高度」屬性來調整矩形的位置和大小,也可以使用新創建的矩形替換現有的矩形。

例如

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

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

分配一個新矩形有用指標。

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] 

所以你需要矩形值存回列表