0

I have problem with setting an image to PictureBox if its empty... I tried few different ways of doing it and I always get same exception. By reading articles here and on the web this should work but it doesn't...

I have declared 6 arrays of PBs...

    PictureBox[] red1 = new PictureBox[4];
    PictureBox[] red2 = new PictureBox[4];
    PictureBox[] red3 = new PictureBox[4];
    PictureBox[] red4 = new PictureBox[4];
    PictureBox[] red5 = new PictureBox[4];
    PictureBox[] red6 = new PictureBox[4];

On FormLoad event those arrays are populated with appropriet PBs...

     PictureBox[] red1 = { pok11, pok12, pok13, pok14 };
     PictureBox[] red2 = { pok21, pok22, pok23, pok24 };
     PictureBox[] red3 = { pok31, pok32, pok33, pok34 };
     PictureBox[] red4 = { pok41, pok42, pok43, pok44 };
     PictureBox[] red5 = { pok51, pok52, pok53, pok54 };
     PictureBox[] red6 = { pok61, pok62, pok63, pok64 };

And when I call method to use them the exception is thrown...

Call:

     DodajSLIKU(Properties.Resources.HERCv2, red1);

Method:

    public void DodajSLIKU(Image slika, PictureBox[] t)
    {            
        if (t[0].Image == null)   //where exception occures.
            t[0].Image = slika;
        else if (t[1].Image == null)
            t[1].Image = slika;
        else if (t[2].Image == null)
            t[2].Image = slika;
        else if (t[3].Image == null)
            t[3].Image = slika;
        else
            return;
    }

Where am I doing it wrong? Thanks...

Iske
  • 976
  • 7
  • 16

1 Answers1

0

Change your method signature like the following:

public void DodajSLIKU(Image slika, PictureBox[] t)
        {
            foreach (var item in t)
            {
                if (item != null)
                {
                    if (item.Image == null)  
                        item.Image = slika;
                }
            }            
        }
sujith karivelil
  • 26,861
  • 6
  • 46
  • 76
  • Using this I don't get Exception any more, but the problem is that item IS null so nothing happens. Is my declaration wrong? I cannot figure that one out... – Iske Dec 16 '15 at 12:03