简体   繁体   中英

c# panel array simplify code

I want to simplify this code. The Code should put all panels (panel1 - panel10) into an panel array

A solution could be with a for loop, but I don't know how to increase the 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;
    }
Panel[] panel = new Panel [] 
{
    panel1,
    panel2,
    panel3,
    ...
    panel10,
};

if panel1..panel10 are directly on the form you can try 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();
 }

Edit: If you have, say, 42 panels the only thing you have to change is the filter 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();
 }

In case you want to organize all the panels with Name like panelNumber (eg panel2 , panel17 , panel347 ...) you can simplify the Where into

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

Create the array with contents like this:

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

An other solution is to use reflection like:

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));
}

Note that this is just an example. If a panel is not available this code will crash due to a null returned. If this could be possible, you need to improve the code a little bit.

Why don't you try Linq? Assuming all the panels are in the form.

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

You can set name 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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM