0

Panel has 90 picturebox as a matrix on my panel.These ll be likely attendance list or you can think these are bus seats and bus has 90 seats. I have 3 png types images which are "green.png" for available seats."red.png" for unavailable seats."blank.png" for if bus has 40 seats 50 picturebox ll be "blank.png".My problem is that,and my question is,how I can fill them automaticaly with giving to function just bus cabacity and available number.

1

for (int i = 8; i <= 97; i++)
{
    var p = Controls.OfType<PictureBox>()
                    .FirstOrDefault(pb => (int)pb.Tag == i);

    p.Image= Image.FromFile("img/blank.png"); ;
}

2

for (int i = 8; i <= 97; i++)
{
     var p = Controls.OfType<PictureBox>()
            .FirstOrDefault(pb => Int32.Parse(pb.Name.Replace("pictureBox", "")) == i);

     p.Image= Image.FromFile("img/blank.png"); 
}

3

int start = 8,finish = 97;
foreach (Panel panel in Controls.OfType<Panel>())
{
    foreach (var pictureBox in Controls.OfType<PictureBox>())
    {
        if (start != finish){
        pictureBox.Image = Image.FromFile("img/blank.png");
        start++;}
    }
}

3rd code is my own code and more clear for me if it could be correct it's better for me and also would be for most of reader too I think,thank you all.. As more clear,just logicaly read this its wrong I know too but you can understand logic from this:

for (i = 8; i <= 50; i++)
    pictureBox{ i}.Image = Image.FromFile("img/green.png");
for (int i = 50; i <= 70; i++)
    pictureBox{ i}.Image = Image.FromFile("img/red.png");
for (int i = 70;i <=90; i++)
    pictureBox{ i}.Image = Image.FromFile("img/blank.png");

I solved problem as :

//x=max size
int start = 0, avaliable = y, unavailable = x - y;
  foreach (Panel panel in Controls.OfType<Panel>())
  {
     foreach (var p in panel.Controls.OfType<PictureBox>())
     {
       if (start <= unavailable)
       {
         p.Image = Image.FromFile("img/green.png");
         start++;
       }
     }
     start = 1;
     foreach (var p in panel.Controls.OfType<PictureBox>())
     {
       if (start <= avaliable)
       {
         p.Image = Image.FromFile("img/red.png");
         start++;
       }
     }
  }
arici
  • 77
  • 8
  • I have no idea what you are actually asking. Please edit with a clear question. – The Sharp Ninja Dec 09 '15 at 21:06
  • I don't understand too. But one thing I would suggest. Remove `continue;` from loop. As it will ignore everything next. – Hamza Hasan Dec 09 '15 at 21:18
  • Could you check again please.. – arici Dec 09 '15 at 22:14
  • 90 PictureBoxes sounds questionable. But if you want to stick with that I would add them to a `List` in the order of their numbers. Then you can access them as `pbList[number].Image = somePng` or `pbList[number].BackColor = Color.Red;` - Searching through the Controls collection repeatedly is bad design for sure. – TaW Dec 09 '15 at 22:53
  • Thank you but one by one filling to array is just crowded i think there should be another way .. – arici Dec 09 '15 at 23:02

0 Answers0