2

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;
    }
Paasi
  • 33
  • 1
  • 5
  • FYI `i++` is a post-increment so the first `arr[i++]` is equivalent to `arr[0]` – phuzi Jun 23 '17 at 08:03
  • To mention: It's a WindowsFormsApplication and on Form1.cs I created 10 panels (like button, textbox etc.) – Paasi Jun 23 '17 at 08:03
  • Possible duplicate of [Get all controls of a specific type](https://stackoverflow.com/questions/4630391/get-all-controls-of-a-specific-type) – Mong Zhu Jun 23 '17 at 08:04

6 Answers6

4
Panel[] panel = new Panel [] 
{
    panel1,
    panel2,
    panel3,
    ...
    panel10,
};
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
ccStars
  • 817
  • 2
  • 11
  • 34
3

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 (e.g. panel2, panel17, panel347...) you can simplify the Where into

 .Where(panel => Regex.IsMatch(panel.Name, "^panel[0-9]+$"))
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
2

Create the array with contents like this:

public Form1()
    {
        InitializeComponent();
        Panel[] arr = new Panel[]{
        panel1,
        panel2,
        panel3,
        panel4,
        panel5,
        panel6,
        panel7,
        panel8,
        panel9,
        panel10
  };
}
Romano Zumbé
  • 7,893
  • 4
  • 33
  • 55
1

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.

Fruchtzwerg
  • 10,999
  • 12
  • 40
  • 49
0

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
Hasan Emrah Süngü
  • 3,488
  • 1
  • 15
  • 33
0

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;
    }
}
Tien Nguyen Ngoc
  • 1,555
  • 1
  • 8
  • 17