2017-06-23 33 views
2

我想簡化這段代碼。代碼應該把所有面板(PANEL1 - panel10)到面板陣列c#面板數組簡化代碼

一個解決辦法是用一個for循環,但我不知道如何增加panelname:

public Form1() 
    { 
     InitializeComponent(); 

     Panel[] arr = new Panel[10]; 

     int i = 0; 
     arr[i] = panel1; 
     arr[i++] = panel2; 
     arr[i++] = panel3; 
     arr[i++] = panel4; 
     arr[i++] = panel5; 
     arr[i++] = panel6; 
     arr[i++] = panel7; 
     arr[i++] = panel8; 
     arr[i++] = panel9; 
     arr[i++] = panel10; 
    } 
+0

FYI'i ++'是一個後增量,所以第一個'arr [i ++]'相當於'arr [0]' – phuzi

+0

提到:這是一個WindowsFormsApplication,在Form1.cs上我創建了10個面板(如按鈕,文本框等) –

+0

可能重複的[獲取特定類型的所有控件](https://stackoverflow.com/questions/4630391/get-all-controls-of-a-specific-type) –

回答

3

如果panel1..panel10是直接在表格上,你可以嘗試的Linq

using System.Linq; 
using System.Text.RegularExpressions; 

... 

public Form1() { 
    InitializeComponent(); 

    // If you want all the panels, remove (comment out) "Where" 
    Panel[] arr = Controls 
    .OfType<Panel>() 
    .Where(panel => Regex.IsMatch(panel.Name, "^panel([0-9]|(10))$")) 
    .ToArray(); 
} 

編輯:如果你有,比如說,42面板你必須改變的唯一事情就是過濾器Where

public Form1() { 
    InitializeComponent(); 

    Panel[] arr = Controls 
    .OfType<Panel>() 
    .Where(panel => { 
     // Given a panel you have to decide should you add it to array or not 
     var match = Regex.Match(panel.Name, "^panel(?<num>[0-9]+)$"); 

     return match.Success && 
       int.Parse(match.Groups["num"].Value) >= 0 && 
       int.Parse(match.Groups["num"].Value) <= 42; }) 
    .ToArray(); 
} 

在你想組織所有的面板與NamepanelNumber情況下(例如panel2panel17panel347 ...)可以簡化成Where

.Where(panel => Regex.IsMatch(panel.Name, "^panel[0-9]+$")) 
+0

謝謝:)這有幫助我! –

+0

如果我有42個面板而不是10個,您可以編輯此代碼嗎? –

+0

@Pascal Niederberger:你唯一需要編輯的就是過濾器'Where' - *你想要組織到數組中的面板。 –

4
Panel[] panel = new Panel [] 
{ 
    panel1, 
    panel2, 
    panel3, 
    ... 
    panel10, 
}; 
2

創建類似這樣內容的數組:

public Form1() 
    { 
     InitializeComponent(); 
     Panel[] arr = new Panel[]{ 
     panel1, 
     panel2, 
     panel3, 
     panel4, 
     panel5, 
     panel6, 
     panel7, 
     panel8, 
     panel9, 
     panel10 
    }; 
} 
0

你爲什麼不嘗試的LINQ?假設所有的面板都在形式中。

var panelArr = Controls.OfType<Panel>(); //Filtering based on Type 
panelArr.Where(p=> Regex.IsMatch(p.Name, "^panel([0-9]|(10))$")) //Filtering based on Panel Name 
panelArr.ToArray(); //Fianlly into Array 
0

您可以設置名稱panel.Name = "pnl" + i.ToString();

public Form1() 
{ 
    InitializeComponent(); 

    Panel[] arr = new Panel[10]; 

    for (int i = 0; i < arr.Length; i++) 
    { 
     Panel panel = new Panel(); 
     panel.Name = "pnl" + i.ToString(); 
     arr[i] = panel; 
    } 
} 
1

另一個解決方法是使用諸如反射:

Panel[] arr = new Panel[10]; 
const string PanelName = "panel"; 
for (int i = 0; i < arr.Length; i++) 
{ 
    FieldInfo pi = GetType().GetField(PanelName + (i + 1), 
     BindingFlags.NonPublic | BindingFlags.Instance); 
    arr[i] = ((Panel)pi.GetValue(this)); 
} 

請注意,這只是一個例子。如果面板不可用,則此代碼將因返回的null而崩潰。如果這可能的話,你需要稍微改進一下代碼。

+1

請加''使用System.Reflection;' –

+0

您需要從1開始而不是從0 –

+0

是的,我更正了這個:'PanelName +(i + 1)' – Fruchtzwerg