c# 面板数组简化代码
c# panel array simplify code
我想简化这段代码。代码应将所有面板(面板 1 - 面板 10)放入面板数组
一个解决方案可能是使用 for 循环,但我不知道如何增加面板名称:
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;
}
如果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();
}
如果您想用 Name
组织所有面板,例如 panelNumber
(例如 panel2
、panel17
、panel347
...),您可以将Where
简化为
.Where(panel => Regex.IsMatch(panel.Name, "^panel[0-9]+$"))
Panel[] panel = new Panel []
{
panel1,
panel2,
panel3,
...
panel10,
};
创建包含如下内容的数组:
public Form1()
{
InitializeComponent();
Panel[] arr = new Panel[]{
panel1,
panel2,
panel3,
panel4,
panel5,
panel6,
panel7,
panel8,
panel9,
panel10
};
}
你为什么不试试 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
您可以设置名称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;
}
}
另一个解决方案是像这样使用反射:
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 - 面板 10)放入面板数组
一个解决方案可能是使用 for 循环,但我不知道如何增加面板名称:
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;
}
如果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();
}
如果您想用 Name
组织所有面板,例如 panelNumber
(例如 panel2
、panel17
、panel347
...),您可以将Where
简化为
.Where(panel => Regex.IsMatch(panel.Name, "^panel[0-9]+$"))
Panel[] panel = new Panel []
{
panel1,
panel2,
panel3,
...
panel10,
};
创建包含如下内容的数组:
public Form1()
{
InitializeComponent();
Panel[] arr = new Panel[]{
panel1,
panel2,
panel3,
panel4,
panel5,
panel6,
panel7,
panel8,
panel9,
panel10
};
}
你为什么不试试 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
您可以设置名称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;
}
}
另一个解决方案是像这样使用反射:
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
而崩溃。如果可能的话,您需要稍微改进一下代码。