2012-04-15 19 views
0

リスト内の特定のインデックスにある要素の値を新しい値に設定しようとしています。C#Rectangle型のリスト内の要素の値を変更する

リストはオブジェクト型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

リストに長方形値をバック格納する必要があります。新しい値を作成する必要があります。 –

答えて

2

性質を私に指示することができますしてください右、左、上、下は読み取り専用です。 OffsetとInflateメソッド、位置、サイズ、幅、高さのプロパティを使用して矩形の位置とサイズを調整するか、既存の矩形を新しく作成したものに置き換えることができます。

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] 

ますので、Rectangleは* *値のタイプである

関連する問題