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