Search code examples
c#winformspicturebox

Show content of a picturebox after mouseclick


I am trying to display a image in pictureBox after I click on it.

First I tried to set PictureBox property for visibility to false and after I click on PictureBox these option come true and the picture should be displayed. It does not work. Please tell me what the best practice for this operation is.

This is my List of PictureBoxes

List<PictureBox> logos = new List<PictureBox>();
//Here how I put Images into every single PictureBox:

 private void f1()
    {
        Insert_Logo();
        PictureBox picture;
        int randomnumber;

        for(int i=0; i< tableLayoutPanel1.Controls.Count; i++)
        {
            if (tableLayoutPanel1.Controls[i] is PictureBox)
            {                      
                    picture = (PictureBox)tableLayoutPanel1.Controls[i];
            }
            else
                continue;

            randomnumber = random.Next(0, logos.Count);
            picture.Image = logos[randomnumber].Image;             
            logos.RemoveAt(randomnumber);                
        }

    }

And this is the function where I tried to display the image after I click on PictureBox:

    private void PictuteBox_CLICK(object sender, EventArgs e)
    {

        PictureBox clickedPicture = sender as PictureBox;

        if(clickedPicture == null)
        {
            return;
        }

        if (firsPic == null)
        {
            firsPic = clickedPicture;
            firsPic.Visible = true;
            return;
        }

    }    

And this is the code for the Insert the pictures in PictureBox:

       private List<PictureBox> Insert_Logo()
    {
        PictureBox pic1 = new PictureBox();
        Image image = Image.FromFile("D:\\Project\\Mini-Game\\Mini-Game\\bin\\Image\\Renault.bmp");
        pic1.Image = image;

        PictureBox pic2 = new PictureBox();
        Image a = Image.FromFile("D:\\Project\\Mini-Game\\Mini-Game\\bin\\Image\\vw.bmp");
        pic2.Size = new Size(a.Width, a.Height);
        pic2.Image = a;

        PictureBox pic3 = new PictureBox();
        Image s = Image.FromFile("D:\\Project\\Mini-Game\\Mini-Game\\bin\\Image\\alfa.bmp");
        pic3.Size = new Size(s.Width, s.Height);
        pic3.Image = s;

        logos.Add(pic1);
        logos.Add(pic2);
        logos.Add(pic3);
        logos.Add(pic4);
        logos.Add(pic5);
        logos.Add(pic6);

return logos;

Solution

  • I think you should write your code like this, explanations are commented

    List<PictureBox> logos = new List<PictureBox>();   
    private List<PictureBox> Insert_Logo()
    {
        PictureBox pic1 = new PictureBox();
        Image image = Image.FromFile("D:\\Project\\Mini-Game\\Mini-Game\\bin\\Image\\Renault.bmp");
        pic1.Image = image;
    
        PictureBox pic2 = new PictureBox();
        Image a = Image.FromFile("D:\\Project\\Mini-Game\\Mini-Game\\bin\\Image\\vw.bmp");
        pic2.Size = new Size(a.Width, a.Height);
        pic2.Image = a;
    
        PictureBox pic3 = new PictureBox();
        Image s = Image.FromFile("D:\\Project\\Mini-Game\\Mini-Game\\bin\\Image\\alfa.bmp");
        pic3.Size = new Size(s.Width, s.Height);
        pic3.Image = s;
    
        logos.Add(pic1);
        logos.Add(pic2);
        logos.Add(pic3);
        return logos;
    }
    
    private void PictuteBox_CLICK(object sender, EventArgs e)
    {
    
        logos = Insert_Logo();
        //The Insert_Logo function returns a list of pictureboxes.
        int randomnumber;
        randomnumber = random.Next(0, logos.Count);
        //Replace Picturebox with the name of the picturebox you want to show the image in
        Picturebox.Image = logos[randomnumber].Image;
        logos.RemoveAt(randomnumber); 
    }