我已經通過stasckoverflows計算出幫助如何循環訪問標籤數組(標籤[] A下面)。我對於如何在同一時間循環標籤A和標籤B的部分感到沮喪,以至於我可以使用相同的代碼。請注意,我可以使用兩個foreach循環,但我計劃要有很多標籤數組(Label [] A - > Labbel [] XXX)。循環遍歷多個標籤數組
Label[] A = { A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13,A14, A15, A16, A17, A18, A19, A20, A21, A22, A23, A24, A25, A26, A27, A28, A29, A30, A31, A32, A33, A34, A35, A36, A37, A38, A39, A40, A50, A51, A52, A53, A54, A55, A56, A57, A58, A59, A60 };
Label[] B = { B1, B2, B3, B4, B5, B6, B7, B8, B9, B10, B11, B12, B13, B14, B15, B16, B17, B18, B19, B20, B21, B22, B23, B24, B25, B26, B27, B28, B29, B30, B31, B32, B33, B34, B35, B36, B37, B38, B39, B40, B50, B51, B52, B53, B54, B55, B56, B57, B58, B59, B60 };
Graphics g = this.CreateGraphics();
Pen pen = new Pen(Color.Blue, 1);
g.DrawRectangle(pen, RectangleDrawer.rect_x, RectangleDrawer.rect_y, RectangleDrawer.rect_w, RectangleDrawer.rect_z);
foreach (Label l in A)
{
if (l.Location.X > RectangleDrawer.rect_x && l.Location.X < (RectangleDrawer.rect_x + RectangleDrawer.rect_w) - 25 &&
l.Location.Y > RectangleDrawer.rect_y && l.Location.Y < (RectangleDrawer.rect_y + RectangleDrawer.rect_z) - 25)
{
l.BackColor = col;
}
任何幫助,你可以提供將不勝感激。
除了我的回答,似乎你實際上已經參考了這120個標籤?您應該在創建它們時將它們添加到列表中(我認爲它是在運行時動態完成的),因此您的代碼將更乾淨。如果您選擇走這條路,同樣可以保存標籤列表。 – SimpleVar 2012-04-19 07:48:00
此外,如果可能的話,給你的標籤的有意義的名稱,而不是隻有A1,A2,A3。它有助於保持您的代碼的組織和可讀性。 – 2012-04-19 08:14:45